Writer macro to change page style to "A5 Landscape"

As a newby to Writer, I have tried to record a macro that changes the current document’s page style to A5 Landscape.
I thought I could do this (as in Microsoft Word) by simply recording a macro and then customising the Writer toolbar to add an icon that would run the macro.
After selecting Tools->Macros->Record Macro, I then input the keystrokes to bring up the Page Style dialog box and make the changes to make paper size and orientation change to A5 Landscape. The document page duly changed accordingly and I then pressed “Stop recording” to finish the macro.
However, when I tried to run the macro, it just brought up the Page Style dialog box for me to make the paper size and orientation changes myself.
Here is the macro that Writer recorded:

sub A5Landscape
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")
rem ----------------------------------------------------------------------
dispatcher.executeDispatch(document, ".uno:PageDialog", "", 0, Array())
end sub

Any suggestions please?
Geoff_B

Windows 7 Home Premium Service Pack 1; LibreOffice version 6.2.4.2 (x64); Build ID: 2412653d852ce75f65fbfa83fb7e7b669a126d64; CPU threads: 8; OS: Windows 6.1; UI render: default; VCL: win; Locale: en-GB (en_GB); UI-Language: en-GB Calc: threaded

Edit (Opaque): Formatted code snippet to “preformatted text”

Sorry, the cut and paste of the macro code seems to have removed all the line breaks!. Here’s what it should look like:

sub A5Landscape

rem ----------------------------------------------------------------------

rem define variables

dim document as object

dim dispatcher as object

rem ----------------------------------------------------------------------

rem get access to the document

document = ThisComponent.CurrentController.Frame

dispatcher = createUnoService(“com.sun.star.frame.DispatchHelper”)

rem ----------------------------------------------------------------------

dispatcher.executeDispatch(document, “.uno:PageDialog”, “”, 0, Array())

end sub

Thanks,
Geoff_B

Just a hint for “preformatted text”: Select the text you want to have in “preformatted text” and click the symbol showing “101010” in the editors toolbar. See my edit of your question.

Thanks Opaque.
In my innocence I thought that “copy and paste” would faithfully reproduce the original, as it does almost everywhere else!
Regards,
Geoff_B

Please forget almost everything you may know about macro recording, when it comes to record macros in LibreOffice. There are many limitations (see here in section “Limitations of the macro recorder”) and almost everything which is performed via mouse interaction will not be recorded. If you plan to use macros then there is one “must read” document to be found here (Andrew Pitonyak Latest Macro Guide).

Thank you Andrew.
Wow, sounds like macros in LibreOffice are someway off being reliable!
I’m fairly familiar with MS Office Visual Basic macro commands - would you suggest that as a good starting point, or are LibreOffice macros so completely different that I should forget all about VB?
Btw, your second link takes me to the website displaying your book entitled “OpenOffice.org Macros Explained”. Are the macro structures, constructs and commands in OpenOffice exactly the same as in LibreOffice?
Regards,

Are the macro structures, constructs and commands in OpenOffice exactly the same as in LibreOffice?

I’m not a developer, so I cannot comment on exactly, but LO inherited everything von AOO and the book ist still THE reference and cited by all people and of some of them I know being developers.

Thanks Opaque.
I think I’ve managed to figure it out now, after much googling!:
The code below seems to work ok:

    sub A5_Landscape
    rem
    rem Set current document page size and orientation to "A5, Landscape" (18 Jul 2019)
    rem
    rem define variables
    Dim oViewCursor as object
    Dim s as string
    Dim oStyle as object
    rem
    rem Set current document width, height and orientation:
    oViewCursor = ThisComponent.CurrentController.getViewCursor()
    s = oViewCursor.PageStyleName
    oStyle = ThisComponent.StyleFamilies.getByName("PageStyles").getByName(s)
    oStyle.Width = 21000
    oStyle.Height = 14800
    oStyle.IsLandscape = True
    rem
    end sub
1 Like

I confirm that the macro works. Cool.