Writer macros to change style properties

I want to write macros to change some of the properties of paragraph and character styles. In particular, highlight by changing the background color.

So I used the Macro Recorder and made the change, and saw that it recorded a call to dispatcher.executeDispatch with specific arguments. I had planned on seeing what the arguments were for changing and changing back, and then writing code to toggle the state.

    ⋮
dim args1(1) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Param"
args1(0).Value = "InnerDialog"
args1(1).Name = "Family"
args1(1).Value = 1

dispatcher.executeDispatch(document, ".uno:EditStyle", "", 0, args1())

But I didn’t get that far. Any attempt to run the macro crashes LibreOffice. I tried the Macro > Run menu, and I tried Run Basic from the code editor.

The empty Main macro that is already present does not crash (though it doesn’t do anything). I see a library BBkode that presumably is used by the extension, and the extension works.

Furthermore, after running LibreOffice again, the macro is missing. I even tried making a new Library for my stuff, and it also is no longer present.

What can I do?

The macro recorder creates dispatcher code, which is not so good. The following API code changes the background color of the paragraph style named MyStyle.

Sub ChangeParaStyle
    oFamilies = ThisComponent.StyleFamilies.getByName("ParagraphStyles")
    oStyle = oFamilies.getByName("MyStyle")
    oStyle.ParaBackColor = RGB(255,141,56)
End Sub

For more examples, see http://www.pitonyak.org/oo.php.

It also sounds like you are not sure how to set up and run macros. There is a tutorial at Writing a Macro in LibreOffice Calc - Getting Started.