[Solved] Macro to check and change the paper size

Creating a macro - Writing a Script - Using the API

[Solved] Macro to check and change the paper size

Postby WilsonLeung » Wed Feb 24, 2016 8:55 am

I have some single document containing both portrait and landscape layout of LongBond paper size(216x330mm)or (8.5x13inch),
It means
Page 1 is Portrait
Page 2 is Landscape
Page 3 is Portrait
Page 4 is Landscape

I want to have a "customized button" to make and change the paper size as below during open the documents

Page 1 is Portrait --------------> change to Portrait (8.5 x12.99 inch)
Page 2 is Landscape --------------> change to Landscape (8.5 x12.99 inch)
Page 3 is Portrait --------------> change to Portrait (8.5 x12.99 inch)
Page 4 is Landscape --------------> change to Landscape (8.5 x12.99 inch)

That means the document size will be changed to (8.5 x12.99 inch) automatically by clicking the "customized button".

Could anyone know how to create a macro script for this ?

Thanks a lot !

Best regards
Attachments
Openoffice-Page Orientation 8.5x13.00.odt
(11.69 KiB) Downloaded 105 times
Last edited by WilsonLeung on Thu Feb 25, 2016 8:10 am, edited 3 times in total.
OpenOffice 4.12 with Win7 64-bit
WilsonLeung
 
Posts: 10
Joined: Fri Jan 22, 2016 12:11 pm

Re: Macro to check and change the paper size

Postby Zizi64 » Wed Feb 24, 2016 9:59 am

I have some single document containing both portrait and landscape layout of LongBond paper size(216x330mm)or (8.5x13inch),


In this case the document have more tha one PAGE STYLES. You can get the existing page styles by your macro and you can adjust the properties of the page styles. There are some example code in the Andrew Pitonyak' books.

and there are similar examples here:
https://forum.openoffice.org/en/forum/v ... 20&t=64546
https://forum.openoffice.org/en/forum/v ... 20&t=31913

and you can find other examples in this Forum...
Tibor Kovacs, Hungary; LO6.1.6 on Win7-10 x64Prof.
PortableApps, winPenPack: LO3.3.0-6.2.5; AOO4.1.5
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 8099
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Macro to check and change the paper size

Postby WilsonLeung » Wed Feb 24, 2016 10:58 am

Hi Zizi64,

Thanks for your advice !
Sorry to let you know i'm no any experience for macro script.

i'm still no ideas how to set the macro for " Landscape" and "Portrait" with PAGE STYLES.
On the other hand, is it possible to use "IF then" function with macro script on this case.
Can you provide more related examples for my reference ?

Thanks a lot !
OpenOffice 4.12 with Win7 64-bit
WilsonLeung
 
Posts: 10
Joined: Fri Jan 22, 2016 12:11 pm

Re: Macro to check and change the paper size

Postby Zizi64 » Wed Feb 24, 2016 11:07 am

Sorry to let you know i'm no any experience for macro script.

Then you will not able to wite a macro for this task... The Macrorecorder can not record the Style modifying activity - as I know it.

You need study the API functions, and must use them in your macros.
Tibor Kovacs, Hungary; LO6.1.6 on Win7-10 x64Prof.
PortableApps, winPenPack: LO3.3.0-6.2.5; AOO4.1.5
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Zizi64
Volunteer
 
Posts: 8099
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Macro to check and change the paper size

Postby musikai » Wed Feb 24, 2016 5:14 pm

Using Page Styles is a good thing.
If you create a new pagestyle based on "Default" then you would just need to change the "Default" page style to your desired new pagesize and all other pagestyles would use it.

But if you already have lots of docs with mixed pagestyles you can try this macro.
Code: Select all   Expand viewCollapse view
sub changeallpagesizesusedindocument
rem Portrait Sizes:
newpagesizeWidth =  21600
newpagesizeHeight =  33000

oViewCursor = ThisComponent.CurrentController.getViewCursor()
oViewCursor.jumptofirstpage(false)

do
s = oViewCursor.PageStyleName
oStyle = ThisComponent.StyleFamilies.getByName("PageStyles").getByName(s)
   if oStyle.IsLandscape=True then
   oStyle.Width = newpagesizeHeight
   oStyle.Height = newpagesizeWidth
   else
   oStyle.Width = newpagesizeWidth
   oStyle.Height = newpagesizeHeight
   end if
loop while oViewCursor.jumptonextpage(false)
oViewCursor.jumptofirstpage(false)
end sub
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/libreofficesongbookarchitect.html
musikai
Volunteer
 
Posts: 232
Joined: Wed Nov 11, 2015 12:19 am

Re: Macro to check and change the paper size

Postby WilsonLeung » Thu Feb 25, 2016 1:05 am

Hi musikai ,

Thank you very much for your help !

May i know how to set the below page margins as well with marco script ?
Page margin:
top=12mm
bottom=12mm
left=10mm
right=10mm

Thanks a lot !
Best regards,
Last edited by WilsonLeung on Thu Feb 25, 2016 2:35 am, edited 1 time in total.
OpenOffice 4.12 with Win7 64-bit
WilsonLeung
 
Posts: 10
Joined: Fri Jan 22, 2016 12:11 pm

Re: Macro to check and change the paper size

Postby musikai » Thu Feb 25, 2016 1:19 am

Ah no! That's too difficult!! :lol:

Code: Select all   Expand viewCollapse view
oStyle.TopMargin
oStyle.BottomMargin
oStyle.LeftMargin
oStyle.RightMargin
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/libreofficesongbookarchitect.html
musikai
Volunteer
 
Posts: 232
Joined: Wed Nov 11, 2015 12:19 am

Re: Macro to check and change the paper size

Postby WilsonLeung » Thu Feb 25, 2016 2:32 am

Hi musikai ,

Sorry that may i know how to input the page margins with your script ?
Page margin:
top=12mm
bottom=12mm
left=10mm
right=10mm

Thanks a lot !

I got an error when run the script below, please advice ...

sub changeallpagesizesusedindocument
rem Portrait Sizes:
newpagesizeWidth = 21590
newpagesizeHeight = 32995

oStyle.TopMargin = 1200
oStyle.BottomMargin = 1200
oStyle.LeftMargin = 1000
oStyle.RightMargin = 1000

oViewCursor = ThisComponent.CurrentController.getViewCursor()
oViewCursor.jumptofirstpage(false)

do
s = oViewCursor.PageStyleName
oStyle = ThisComponent.StyleFamilies.getByName("PageStyles").getByName(s)
if oStyle.IsLandscape=True then
oStyle.Width = newpagesizeHeight
oStyle.Height = newpagesizeWidth
else
oStyle.Width = newpagesizeWidth
oStyle.Height = newpagesizeHeight
end if

loop while oViewCursor.jumptonextpage(false)
oViewCursor.jumptofirstpage(false)

End Sub
OpenOffice 4.12 with Win7 64-bit
WilsonLeung
 
Posts: 10
Joined: Fri Jan 22, 2016 12:11 pm

Re: Macro to check and change the paper size

Postby musikai » Thu Feb 25, 2016 3:08 am

Hi,

yeah, cool that you are trying. You just have to put it at another place.
The settings at the top are only for convenience so you don't have to set everything twice (for portrait and landscape).

As I don't know if you want the margins to be the same on portrait and landscape you might have to correct the code below.
It is set that Landscape is like Portrait rotated 90° right (clockwise).

Code: Select all   Expand viewCollapse view
sub changeallpagesizesusedindocument
rem Portrait Sizes:
newpagesizeWidth = 21590
newpagesizeHeight = 32995
newTopMargin = 1200
newBottomMargin = 1200
newLeftMargin = 1000
newRightMargin = 1000


oViewCursor = ThisComponent.CurrentController.getViewCursor()
oViewCursor.jumptofirstpage(false)

do
s = oViewCursor.PageStyleName
oStyle = ThisComponent.StyleFamilies.getByName("PageStyles").getByName(s)
if oStyle.IsLandscape=True then   'this is for Landscape, the margins as if we rotated portrait 90°right, change as you like
oStyle.Width = newpagesizeHeight
oStyle.Height = newpagesizeWidth
oStyle.TopMargin = newLeftMargin
oStyle.BottomMargin = newRightMargin
oStyle.LeftMargin = newBottomMargin
oStyle.RightMargin = newTopMargin
else    'this is for portrait
oStyle.Width = newpagesizeWidth
oStyle.Height = newpagesizeHeight
oStyle.TopMargin = newTopMargin
oStyle.BottomMargin = newBottomMargin
oStyle.LeftMargin = newLeftMargin
oStyle.RightMargin = newRightMargin
end if

loop while oViewCursor.jumptonextpage(false)
oViewCursor.jumptofirstpage(false)

End Sub
Win7 Pro, Lubuntu 15.10, LO 4.4.7, OO 4.1.3
Free Project: LibreOffice Songbook Architect (LOSA)
http://struckkai.blogspot.de/2015/04/libreofficesongbookarchitect.html
musikai
Volunteer
 
Posts: 232
Joined: Wed Nov 11, 2015 12:19 am

Re: Macro to check and change the paper size

Postby WilsonLeung » Thu Feb 25, 2016 3:26 am

Hi musikai,

Great ! thank you for your kindness and help !

Best regards,
OpenOffice 4.12 with Win7 64-bit
WilsonLeung
 
Posts: 10
Joined: Fri Jan 22, 2016 12:11 pm


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 5 guests