Greg was telling us:
Greg nous racontait que :
Or a little cleaner:
Sub Test()
Dim oLinkHeader
Dim oLinkFooter
With Selection
oLinkHeader =
.Sections(1).Headers(wdHeaderFooterPrimary).LinkToPrevious
oLinkFooter =
.Sections(1).Footers(wdHeaderFooterPrimary).LinkToPrevious
.Bookmarks("\Page").Select
If .Sections(1).Index < ActiveDocument.Sections.Count Then
.MoveEnd Unit:=wdCharacter, Count:=-1
.Collapse wdCollapseEnd
If .End = .Sections(1).Range.End - 1 Then
.InsertBreak Type:=wdSectionBreakNextPage
.PageSetup.Orientation = wdOrientLandscape
oLinkHeader = False
oLinkFooter = False
Else
.InsertBreak Type:=wdSectionBreakNextPage
.PageSetup.Orientation = wdOrientLandscape
oLinkHeader = False
oLinkFooter = False
.InsertBreak Type:=wdSectionBreakNextPage
.PageSetup.Orientation = wdOrientPortrait
oLinkHeader = False
oLinkFooter = False
End If
Else
.Collapse wdCollapseEnd
If .End = ActiveDocument.Range.End - 1 Then
.InsertBreak Type:=wdSectionBreakNextPage
.PageSetup.Orientation = wdOrientLandscape
oLinkHeader = False
oLinkFooter = False
End If
End If
End With
End Sub
You have opened a real can of worms. ;-)
I did the exact same thing for a client 2 years ago.
It was a but more complicated because the first page of the Annex section
had to be preserved... but essentially, it was the same.
The OP has not considered the case when the current page finishes with a
manual page break... that can create unwanted effects (Namely, a blank page
after the newly inserted Landscape section).
Also, what if the current section is already landscape?
If the cursor is exactly before the last ¶ in the document, then the "\Page"
bookmark generates an error.
If you add a landscape section after section one, the landscape section is
now section 2 and what was section 2 is now section 3. If you remove the
"Same as previous" attribute on the landscape section, that's OK, but then
section 3 will have the landscape section header/footer because it is still
set to "Same as previous." So, before inserting a section you have to remove
the "Same as previous" attribute in the following section, and then insert
the landscape section...
Also, I do not understand what
oLinkHeader = False
oLinkFooter = False
do in your code. They are Boolean variables (Even tough they are not
assigned as such...

) that take the value of the initial section
header/footer "Same as previous" attribute. Then they are always set to
false, but the inserted section is not affected... So I do not understand
the purpose for these lines of code.
Anyway, when I was finished, I had hundreds of line of code...If I remove
the constraints I had regarding some pages that were untouchable (Like the
first two in the document, the first page of the Annexe, and the nightmare
regarding the preservation of headers/footers, then I would still have a lot
of code, maybe that is because I went overboard with error trapping... I
tried to foresee every possible situation so that the client could not
comeback and say "When I do this, it screws up my document..."
--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site:
http://www.word.mvps.org