How to change a forms background colour at runtime?

In Writer one can change the background colour while in normal writing mode. Is there a way by way of macro to change a base forms background colour at runtime?

Thank you

Provided you are not using custom styles, the color is in the Standard style:

oStdPage = ThisComponent.getStyleFamilies().getByName("PageStyles").getByName("Standard")
oStdPage.BackColor = RGB(255, 50, 100)  Rem - change for desired color.

See Apache OpenOffice Community Forum - [Solved] Using a macro how do I change a color of a form - (View topic)

Edit 2022-08-03:

Posting two methods for saving the style data when using Base.

Many open issues with Base making it necessary to work around the bugs. For one, open in Design view button (form editing) does not work. It is set in the config file but does nothing. Tried back to 4.x version. So to do this the form must not be opened in the normal manner. The attached Base file has an added menu item - Edit Form. Click then select - Open ITEMS. On the form is a list box to select a change in background color.

An cleaner method is to use a Standalone form. This can be set to Edit without opening in Design mode. This form has the database connection left blank but can easily be attached to the Base file provided here. However, it is not necessary to demo the color changing. The list box works. Edit the form and change property to conect to Base:

Screenshot at 2022-08-03 17-34-04

Point to location of Base file downloaded and insure the table is selected in Content.

ColorChanging.odb (18.8 KB)
StandaloneColorDoc.odt (15.0 KB)

Thank you so much, the color changes however the change does not stick. If you close and then reopen the form the original colour is returned. Is it possible to make it stick?

One way is to assign macro from @Ratslinger to the form event When Loading (Menu / Form / Form Properties, tab Events).

1 Like

Brilliant, thanks for that.

If anyone has a need for this one soluions is some button bar on the form you want to change the background colour


each button will have a macro attached which will update a filtertable

sub black
oConn = ThisDatabaseDocument.DataSource.getConnection("","")
result= SQL.executeQuery(“UPDATE ““tblFilter”” SET ““bcolour”” = ‘Black’”)
oForm = ThisComponent.Drawpage.Forms.getByName(“frmMain”)
End Sub

To retrieve the colour on the form you need a macro on the on load and on the on reload event

Sub Color_frmUtil
rem all forms for this macro need to be called frmMain and the sub to be attached to on load and on reload events
oConn = ThisDatabaseDocument.DataSource.getConnection("","")
dim SQL as object
dim dco AS String
oConn = ThisDatabaseDocument.DataSource.getConnection("","")
result=SQL.executeQuery(“SELECT ““bcolour”” FROM ““tblFilter””” )
dco = result.getString(1)
’ msgbox " " & dco

oDoc = ThisComponent
oDrawPage = oDoc.DrawPage
oForm = oDrawPage.Forms.GetByName(“frmMain”)

if dco = “Black” then
oVCurs = oDoc.CurrentController.getViewCursor()
oPageStyleName = oVCurs.PageStyleName
oPageStyles = oDoc.StyleFamilies.getByName(“PageStyles”)
oStyle = oPageStyles.getByName(oPageStyleName)
oStyle.BackColor = RGB(0, 0, 0)
End If
End Sub

To change a colour in the macro adjust the rgb values

Please see my edited answer.