I’m always skeptical when I read the macros via Ctrl+CV without bigger understanding of the macro code are put to bigger project with expectation that will be functional without chaos, but it seems it could be “functional” if people will not devise the stupid operations like “I deliberately do something wrong with it, and I will spy how it will behave” - that is probably typical behavior for many Czech people :-); and next phase is ridicule “Hahahááá, it failed :-)))!!”; and next is inner reinforcement “Haháá, the authors are cretins and I’m much better that I spoiled it (and absolutely never mind that I never made something functional, simply I’m much better!!)”
.
I think the Macro Book from A. Pitonyak (previous link) is really good source to start the learning of programming with macros in LibreOffice. But of course, it tooks a lot of time for beginners.
Here is small explanation you didn’t know, I hope for good start :-).
Sub
or Function
could have parameters and mostly have parameters → Sub macro1(paramater1, parameter2, parameter3 ...)
So if you create the Sub
with parameters, you need call it with parameters. Exception is if you define the parameters as optional
→ Sub macro2(optional sParam as string)
→ it that case you can run only macro2
withou parameter sParam
.
If you assing the macro to the button, then it isn’t possible to put parameters to this macro. It is possible only to catch the button event as parameter, but it will be described later.
So for example you have Button1 and you assign the macro macro1 to this button:
Sub macro1 'macro that will run after click on button
dim sName$
sName="End" 'the name of bookmark
'...
End Sub
And you will have also Button2. And the question is, if you will assign the same macro or if you will create macro2.
Because if you will assign the same macro, then variable sName will stay set on End.
So the 1st solution can be: make 2 macros, assign ones to 2 buttons, and make 3rd Sub that is called from button-macros but with different parameter.
Sub macroButton1 'macro for Button1
jumpToBookmark("End")
End Sub
Sub macroButton2 'macro for Button2
jumpToBookmark("Start")
End Sub
Sub jumpToBookmark(sName$) 'it jumps to the bookmark that's name is in parameter sName
dim oDoc as object, oBookmark as object
oDoc=ThisComponent
'...
End Sub
Or it is possible to assign one macro for more buttons, and use the button event that is given automatically as single parameter to macro for clicked button → and then call execute function for example according to names of buttons (but it supposes the unique name for every button).
Both examples are in ODT, 1st for upper buttons, 2nd for bottom buttons.
macros-for-buttons.odt (44.6 kB)