PDA

View Full Version : Page layout change from portrait to landscape (vice versa) via VBA?



Mark2005
03-15-2006, 01:22 AM
Hi everybody,

I got a problem with changing the paper layout via Word-VBA (Word XP = 2000 = V10.0). It is a little bit tricky and so far nobody could help me (and I am looking for a solution many weeks yet). :banghead:
The result should be a VBA-Code, which changes the current side of a Word Document from its current layout to the other (e.g. portrait -> landscape or if it is currently landscape -> portrait). The user presses an Icon and the change of the layout should take place. And only the current page should changed - nothing else (not the pages before and not the pages after...).
So the result should be:
Makro1: ChangeToPortrait ()
Makro2: ChangeToLandscape ()
For the beginning it would be enough, when the current page and the rest will be changed.
I can easy do it manually, therefore there must be a solition to do it automatically.
A resolution for Word2003 is:


Sub ChangeToLandscapeLayout()
ActiveDocument.Range(Start:=Selection.Start, End:=Selection.Start). _
InsertBreak Type:=wdSectionBreakNextPage
Selection.Start = Selection.Start + 1
ActiveDocument.Range(Start:=Selection.End, End:=Selection.End).InsertBreak _
Type:=wdSectionBreakNextPage
With Selection.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientLandscape
End With
End Sub

but this is not working for Word XP (2000).
I can image the following could work (but it wasn't so far).
mark all at current page (=> don't know how to do it)
change layout (for that paragraph)
unmark all
Do you think it is possible in that way?


Thanks for you help

TonyJollans
03-15-2006, 04:55 AM
This one is quite a challenge to do cleanly and I don't have time to give to it right now - I will try later.

Word V10 is actually Word 2002 (a component of Office XP) - is this the version you are having trouble with? Or is it Word 2000 (which is V9)? Whichever, what is it that actually happens (or doesn't)?

Mark2005
03-15-2006, 06:02 AM
Sorry, my mistake. It is 2002 not 2000 of course...

Mark2005
04-27-2006, 02:27 AM
This is a first attempt:
http://vbaexpress.com/forum/showthread.php?t=7756 (http://vbaexpress.com/forum/showthread.php?t=7756)

Unluckily, it is inserting (Word XP) always two pages. And it is really tricky to delete the non desired page to get all correctly (as it should be: only one page in the desired format added). There can be so many different situations.
e.g. You got only one page.
Your page is already to the desired format / layout.
You stand at the beginning of the document.
You are at the end....

Unluckily - always a different behavior for this entire examples is necessary... (means: for all possible situation a different code is needed)


M.

AndrewPerl
04-27-2006, 11:28 AM
Mark,

The link in one of the previous posts goes to a discusson thread that I started. I have a similar issue. My assumption is that the user will want to insert a landscape page somewhere in the middle of a document. The code sample (that works by the way) inserts the section breaks, changes to page prientation and margins, and reformats the header and footer. I am looking at a way to make the header/footer code more elegant. All of this works under Word XP.

You can use the code as a starting point. I would be happy to answer any questions you may have.

Andrew Perl
Kansas City

fumei
04-27-2006, 10:13 PM
Could you please clarify precisely what you want to happen?
mark all at current page (=> don't know how to do it)
change layout (for that paragraph) 1. I do not know what you really mean by mark all at the current page.

2. change layout for that paragraph? No. Not possible. Layouts are by SECTION. It does not apply to a page. It applies to a Section, regardless of how many pages are in that section. Actually, there is no "page" in Word. If you want what is calculated to be a page to be in a differnt layout (portrait / lanscape) you must change the Section the page is in. Therefore if you want a single page to be different, it MUST be in its own section. No ifs, ands, or buts - it must be in its own section.

So, could you please clarify precisely what your needs are?

Mark2005
04-27-2006, 11:37 PM
Mark,

The link in one of the previous posts goes to a discusson thread that I started. I have a similar issue. My assumption is that the user will want to insert a landscape page somewhere in the middle of a document. The code sample (that works by the way) inserts the section breaks, changes to page prientation and margins, and reformats the header and footer. I am looking at a way to make the header/footer code more elegant. All of this works under Word XP.

You can use the code as a starting point. I would be happy to answer any questions you may have.

Andrew Perl
Kansas City


HI Andrew,

thx for you reply. Of course I tested the code, before I wrote my answer. What happens in my Word Version (it is XP = 2002 = 10.4219.4219 SP2 - says at least the help->info).
And what happens, when start the makro, is that two new pages are inserted in the document. Maybe it is due to of the page-break. I heard that that is not the case with other Word-versions... and as you wrote it should work, but I cant' confirm so far (I haven't tested it)...

Luckily I found the thread of you, because I haven't thought that almost the same issue will start again after my request, because nobody could help me solving it the last six weeks. BTW: I got that issue already a few month - found no real good solution so far (of Word-XP I'm talking about of course).


Greets Mark

Mark2005
04-27-2006, 11:54 PM
Could you please clarify precisely what you want to happen?1. I do not know what you really mean by mark all at the current page.

2. change layout for that paragraph? No. Not possible. Layouts are by SECTION. It does not apply to a page. It applies to a Section, regardless of how many pages are in that section. Actually, there is no "page" in Word. If you want what is calculated to be a page to be in a differnt layout (portrait / lanscape) you must change the Section the page is in. Therefore if you want a single page to be different, it MUST be in its own section. No ifs, ands, or buts - it must be in its own section.

So, could you please clarify precisely what your needs are?

Hello Fumei!

I try my best to make i clear :beerchug:

well I know, that I can't set the layout for each page with a script on a easy way (that is my main problem). Therefore the idea was to simulate the way as I do it, when I make everything manually (- by hand).

That means to mark the part, which should be changed (than make a new section out of it and then change the layout only for that new section) -> And as I want to change the layout for a page, I thought you need to mark all at the current page (but maybe that also doesn't not give a solution - as said, just an idea).
And as I wrote in the starting message. For Word 2003 the code, I copied there is working (me was told at least).

Anyway: What I want at the end of the day.
A Makro, which does the following when you start it:
Change the current layout of the whole page (where the cursor currently is -> current position -> that page should be changed) to another layout. And vice versa.

The algorithm of that - (how to is not important) - it only should work (for Word-XP)


Thx Mark
BTW: With the following statements you can jump to the next page resp. to the previous page

Application.Browser.Next
Application.Browser.Previous


This sets the cursor to the first Character of a page and marks it

Selection.Bookmarks("\Page").Range.Characters.First.Select


and that jumps to the first Character of a page

Selection.Bookmarks("\Page").Select
Selection.Collapse wdCollapseStart

But I don't know yet, how to set a marking from the first character of a page the last (= first char of next page - 1)

Mark2005
04-28-2006, 12:33 AM
BTW - I found out how to mark a specific page: But so far, I doens't help me to change the layout.
Anybody an idea how to make it (mabye with help of the following statements)?


Function getPageNumbers() As Long
getPageNumbers = ActiveDocument.ComputeStatistics(wdStatisticPages)
End Function
Function getCurrentPage() As Long
ActiveDocument.ComputeStatistics wdStatisticPages
getCurrentPage = Selection.Information(wdActiveEndPageNumber)
End Function
Function getPageStartOfMarking() As Long
ActiveDocument.ComputeStatistics wdStatisticPages
getPageStartOfMarking = ActiveDocument.Range(Selection.Range.Start, _
Selection.Range.Start).Information(wdActiveEndPageNumber)
End Function
Sub setCursorToPage(Page As Long)
ActiveDocument.ComputeStatistics wdStatisticPages
Selection.GoTo What:=wdGoToPage, Name:=Page
End Sub
Sub MarkPage(Page As Long)
Dim oRange As Range
ActiveDocument.ComputeStatistics wdStatisticPages
Selection.GoTo What:=wdGoToPage, Name:=Page
Set oRange = Selection.Bookmarks("\Page").Range
oRange.Select
End Sub
Sub Test()
MsgBox getPageNumbers
MsgBox getCurrentPage
MsgBox getPageStartOfMarking
MarkPage (getCurrentPage)
End Sub

AndrewPerl
04-28-2006, 02:08 PM
Mark,

As Fumei stated in an earlier post; the only way you can change page orientation is to add a section break. The problem with adding one section break is that all following pages will be landscape. The best way is to add two section breaks and then change the page orientation of the new section. The code in my thread also deals with the headers and footers. Beware of the same as previous functionality when dealing with header and footers and page orientation.

Andrew Perl
Kansas City

lucas
04-28-2006, 06:34 PM
Hi Andrew,
Haven't been following this too closely but this will insert a landscape page....

Sub InsertLand()

' This macro inserts a landscaped page, followed by a portrait page; another favorite.

Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.MoveUp Unit:=wdLine, Count:=1

With Selection.PageSetup

.Orientation = wdOrientLandscape
.TopMargin = InchesToPoints(1)
.BottomMargin = InchesToPoints(1.25)
.LeftMargin = InchesToPoints(1)
.RightMargin = InchesToPoints(1)
.Gutter = InchesToPoints(0)
.PageWidth = InchesToPoints(11)
.PageHeight = InchesToPoints(8.5)
.SectionStart = wdSectionNewPage

End With

Selection.TypeParagraph
Selection.TypeParagraph
Selection.MoveUp Unit:=wdLine, Count:=1

End Sub

fumei
04-28-2006, 10:46 PM
That means to mark the part, which should be changed (than make a new section out of it and then change the layout only for that new section) -> And as I want to change the layout for a page, I thought you need to mark all at the current page (but maybe that also doesn't not give a solution - as said, just an idea).You MUST grasp this: you can NOT change the layout for a page. You can only change the layout for a section.

You can NOT change the layout for a page.
You can ONLY change the layout for a section.

This issue was covered extensively - and code supplied - in the other thread.
BTW - I found out how to mark a specific page: But so far, I doesn't help me to change the layout.
Anybody an idea how to make it (mabye with help of the following statements)?This is pointless. Yes, you can mark a specific page - and by the way, you can do that much much easier than the code you have there - but it still does not make any difference. As you question and note....it does NOT help you change the layout. That is because:

You can NOT change the layout of a page.
You can ONLY change the layout of a section.

Mark2005
05-02-2006, 12:12 AM
You MUST grasp this: you can NOT change the layout for a page. You can only change the layout for a section.

You can NOT change the layout for a page.
You can ONLY change the layout for a section.

This issue was covered extensively - and code supplied - in the other thread. This is pointless. Yes, you can mark a specific page - and by the way, you can do that much much easier than the code you have there - but it still does not make any difference. As you question and note....it does NOT help you change the layout. That is because:

You can NOT change the layout of a page.
You can ONLY change the layout of a section.


OK as it seems, it is only a problem with WordXP. I tested it with Word2003 and it is working fine.

BTW: I understood that I can't set it for a page only for a section - that's why I was asking here in that forum (if it would work for pages, I would have never had a problem). But this idea is working for all later Versions! Therefore I graped it already...
But as I wrote already a several times, my idea was to make out of a page a section! I don't know why you can't understand that... Where the page start: Sectionstart. Where the page ends: Sectionend! And nothing else.
But it is also not working in that way as it should (at least not for my current Version). Anyway - as it is not working only with my version, I'm fed up with that, because than I think it is useless and not worth to do it. Just buy and install a newer version and all my probs in that case a fixed (and I din't care anymore if it has to be a section or not - because the desired result is given).

:giggle

Thanks to everybody trying to help me!
@fumei
AND BTW: ...other thread - please check the dates, than you would see, my thread was started earlier than the other... And I wrote that this is not solving my problem. And if you would have read my starting thread, you would have understood, that I got already a solution, but that was not working with this W-XP! You grap it now? The main algorith in the later thread (you mentioned) is the same as I posted at the beginning... The other guys at least did! And if you think my little example is to complicated, then please give my a better solution. So please stop commenting my stuff, it is not very helpfull what you write!

fumei
05-09-2006, 11:20 PM
I am sorry.

I can not duplicate your problem with Word XP (2002). You are saying you have a code solution that works for 2003, but does not for 2002? Could you please post that?

As for making any page into a section - so you can change from portrait to landscape:Sub MakePageASection()
Dim r As Word.Range
Set r = ActiveDocument.Bookmarks("\page").Range
r.InsertBreak Type:=wdSectionBreakNextPage
Set r = Selection.GoToNext(what:=wdGoToPage)
Set r = ActiveDocument.Bookmarks("\page").Range
r.InsertBreak Type:=wdSectionBreakNextPage

Set r = Selection.GoToPrevious(what:=wdGoToPage)
Set r = ActiveDocument.Bookmarks("\page").Range
With r.PageSetup
.Orientation = wdOrientLandscape
End With
Set r = Nothing
End SubThis makes the page the Selection is on become a single section. I know, I know...it looks weird. However, there is no error trapping for what causes the page "break". If the current page has a "hard" page break, rather than a gee-I-ran-out-of-room-so-I-need-new-page break, you will still get a section break. Which would mean a double break. I notice you did not have any error trapping in your code, so I will assume that there would be no hard page breaks. It would be fairly easy to test for page conditions, if required. (Not a bad idea generally.)