VBA: Switch Landscape or Portrait Using 1 Command/Button

Susanto3311
3StarLounger
Posts: 240
Joined: 17 Feb 2022, 05:16

VBA: Switch Landscape or Portrait Using 1 Command/Button

Post by Susanto3311 »

hi all..

i found code to set orientation paper in landscape/portrait.
the code not properly work

Code: Select all

Sub Landscape()
With ActiveDocument.PageSetup
   .Orientation = wdOrientLandscape
   .Orientation = wdOrientPortrait
End With
End Sub

using button (quick Acees Toolbar) with the above code i want to switch Landscape or Portrait
using only single button macro, if possible to do it?
sometimes i want to set switch landscape (with 1 click button), then 1 click button again to change in Portrait
maybe someone give assistance to figure out this case.

thank in advance

susanto

User avatar
StuartR
Administrator
Posts: 12602
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: VBA: Switch Landscape or Portrait Using 1 Command/Button

Post by StuartR »

Code: Select all

Sub TogglePageOrientation()

With ActiveDocument.PageSetup
    If .Orientation = wdOrientLandscape Then
        .Orientation = wdOrientPortrait
    Else
        .Orientation = wdOrientLandscape
    End If
End With

End Sub
StuartR


Susanto3311
3StarLounger
Posts: 240
Joined: 17 Feb 2022, 05:16

Re: VBA: Switch Landscape or Portrait Using 1 Command/Button

Post by Susanto3311 »

hi StuartR, thank you so much!!
working well..

User avatar
Charles Kenyon
5StarLounger
Posts: 609
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: VBA: Switch Landscape or Portrait Using 1 Command/Button

Post by Charles Kenyon »

Note that if you use this it applies to the entire document, unlike the UI control which only applies to the current section.

User avatar
StuartR
Administrator
Posts: 12602
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: VBA: Switch Landscape or Portrait Using 1 Command/Button

Post by StuartR »

Good point Charles
StuartR


User avatar
SpeakEasy
4StarLounger
Posts: 544
Joined: 27 Jun 2021, 10:46

Re: VBA: Switch Landscape or Portrait Using 1 Command/Button

Post by SpeakEasy »

If you like brief/obscure code:

Code: Select all

ActiveDocument.PageSetup.Orientation = Abs(ActiveDocument.PageSetup.Orientation <> wdOrientLandscape)

User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA: Switch Landscape or Portrait Using 1 Command/Button

Post by HansV »

Or even shorter if you are sure that the entire document has the same orientation:

Code: Select all

ActiveDocument.PageSetup.Orientation = 1 - ActiveDocument.PageSetup.Orientation
Best wishes,
Hans

User avatar
SpeakEasy
4StarLounger
Posts: 544
Joined: 27 Jun 2021, 10:46

Re: VBA: Switch Landscape or Portrait Using 1 Command/Button

Post by SpeakEasy »

:grin:

User avatar
StuartR
Administrator
Posts: 12602
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: VBA: Switch Landscape or Portrait Using 1 Command/Button

Post by StuartR »

I prefer comprehensibility to brevity, I have had to spend too much time maintaining other people's obscure code in the past.
StuartR


User avatar
HansV
Administrator
Posts: 78416
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA: Switch Landscape or Portrait Using 1 Command/Button

Post by HansV »

I fully agree with that.
Best wishes,
Hans

User avatar
SpeakEasy
4StarLounger
Posts: 544
Joined: 27 Jun 2021, 10:46

Re: VBA: Switch Landscape or Portrait Using 1 Command/Button

Post by SpeakEasy »

Yep, that's why a made sure I pointed out it was an obscure bit of code

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15587
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: VBA: Switch Landscape or Portrait Using 1 Command/Button

Post by ChrisGreaves »

Charles Kenyon wrote:
08 Mar 2022, 14:45
Note that if you use this it applies to the entire document, unlike the UI control which only applies to the current section.
Well spotted Charles! :clapping:

Always with the unanticipated mysteries, Charles. You should write Mystery Thriller Fiction!
And here I was thinking that, if I knew nothing else, I knew Word/VBA :yikes:
Cheers
Chris
There's nothing heavier than an empty water bottle

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15587
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: VBA: Switch Landscape or Portrait Using 1 Command/Button

Post by ChrisGreaves »

HansV wrote:
08 Mar 2022, 19:01
Or even shorter if you are sure that the entire document has the same orientation:
But as Charles has pointed out, that would takke all the fun out of life, wouldn't it? :evilgrin:
Cheers
Chris
There's nothing heavier than an empty water bottle