Test for End of Section

D

Debra Ann

I can't tell all of you how much help you have been to me in learning how to
create VB code. I would truely be lost without you all.

In my code, I select the page and then test to see if my selection is at the
end of the document by testing with:
If Selection.Type = wdSelectionIP And Selection.End = _
ActiveDocument.Content.End - 1 Then ...

Now I need to test if the selected page is the last page of a section or if
there are more text / manual pages following the selected page. Is there a
way to test if the current selected page is the end of a section?

Thanks again!!!
 
D

Doug Robbins - Word MVP

If Selection.Type = wdSelectionIP And Selection.End =
Selection.Sections(1).Range.End - 1 Then
MsgBox "At end of Section"
Else
MsgBox "Not at end of Section"
End If


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

Debra Ann

I implemented your suggestion but it still doesn't work when I test it on the
last page of a section. It should be creating just one section break if I am
on the last page of a section and it does not. It creates two. Here is my
code:

Selection.Bookmarks("\Page").Select

If Selection.Type = wdSelectionIP And Selection.End = _
ActiveDocument.Content.End - 1 Then
Selection.Collapse wdCollapseEnd
Selection.InsertBreak Type:=wdSectionBreakNextPage
Else
If Selection.Type = wdSelectionIP And Selection.End = _
Selection.Sections(1).Range.End - 1 Then
Selection.Collapse wdCollapseEnd
Selection.InsertBreak Type:=wdSectionBreakNextPage
Else
Selection.Collapse wdCollapseEnd
Selection.MoveUp
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.MoveUp
End If
End If

Any suggestions? The test with the end of document works and creates just
one section break but the nested if doesn't work.

Thanks
 
G

Greg

Debra,

I don't see how your code worked except for the final Else piece. If
you select a page then there is no way the selection can be type IP.

I am not sure what you are trying to achieve, but if it is to insert a
section break at the end of a section or the document, you might be
able to adapt the following.

Sub Test()
Selection.Bookmarks("\Page").Select
If Selection.Sections(1).Index < ActiveDocument.Sections.Count Then
Selection.MoveEnd Unit:=wdCharacter, Count:=-1
Selection.Collapse wdCollapseEnd
If Selection.End = Selection.Sections(1).Range.End - 1 Then
Selection.InsertBreak Type:=wdSectionBreakNextPage
End If
Else
Selection.Collapse wdCollapseEnd
If Selection.End = ActiveDocument.Range.End - 1 Then
Selection.InsertBreak Type:=wdSectionBreakNextPage
End If
End If
End Sub
 
T

Tony Jollans

If you are selecting a page then the selection type will not be an
insertion point, so remove that check and try just ..

Selection.Bookmarks("\Page").Select
If Selection.End = Selection.Sections(1).Range.End Then
 
D

Debra Ann

Here is what I was trying to do. I am creating a button that will insert a
landscape page on the next page of the document from where the user's cursor
is sitting when he hits the button. There are three possible issues that can
affect this (actually four but in previous code I am not letting him sit at
the beginning of the first three sections of the document so the issue of
being on the first page is out):

If I have the macro select the current page, the cursor will be sitting
either:

1. At the bottom of the page because it is the last page of the document.
2. At the top of the next page in the middle of a section.
3. at the top of the next page but at the beginning of a new section.

If (1) occurs, then I want to create "one" section break, shut off the same
as previous for that "one" page, and create the landscape page.

If (2) occurs, then I want to create "two" section breaks, shut off the same
as previous for the "current and next" section, and create the landscape page.

If (3) occurs, then I want to create "one" section break, shut off the same
as previous for the "current and next" section, and create the landscape page.

This is my dilemma. I was originally testing for the first two only and it
worked fine. But then I realized I didn't want two section breaks if I was
already at the beginning of a new section.
 
G

Greg

Try this:

Sub Test()
Selection.Bookmarks("\Page").Select
If Selection.Sections(1).Index < ActiveDocument.Sections.Count Then
Selection.MoveEnd Unit:=wdCharacter, Count:=-1
Selection.Collapse wdCollapseEnd
If Selection.End = Selection.Sections(1).Range.End - 1 Then
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.PageSetup.Orientation = wdOrientLandscape
Selection.Sections(1).Headers(wdHeaderFooterPrimary).LinkToPrevious
= False
Selection.Sections(1).Footers(wdHeaderFooterPrimary).LinkToPrevious
= False
Else
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.PageSetup.Orientation = wdOrientLandscape
Selection.Sections(1).Headers(wdHeaderFooterPrimary).LinkToPrevious
= False
Selection.Sections(1).Footers(wdHeaderFooterPrimary).LinkToPrevious
= False
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.PageSetup.Orientation = wdOrientPortrait
Selection.Sections(1).Headers(wdHeaderFooterPrimary).LinkToPrevious
= False
Selection.Sections(1).Footers(wdHeaderFooterPrimary).LinkToPrevious
= False
End If
Else
Selection.Collapse wdCollapseEnd
If Selection.End = ActiveDocument.Range.End - 1 Then
Selection.InsertBreak Type:=wdSectionBreakNextPage
Selection.PageSetup.Orientation = wdOrientLandscape
Selection.Sections(1).Headers(wdHeaderFooterPrimary).LinkToPrevious
= False
Selection.Sections(1).Footers(wdHeaderFooterPrimary).LinkToPrevious
= False
End If
End If
End Sub
 
G

Greg

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
 
D

Debra Ann

Thanks Greg. I'm running into a meeting that will be most of the day. I'll
try if over the weekend and let you know on Monday.
 
J

Jean-Guy Marcil

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
 
G

Greg

Uummm..., I guess I stepped in it with both feet <ssg> that's "sheepish
stupid grin"

I suppose my only saving grace was like Debra, I was rushing off to
something else as well. Fortunately my poor services rendered were
"free" and there is no client fee to return.

"oLinkHeader = False" was a complete brain puff attempt to abbreviate
the code.
Back to the drawing board on that.

Sorry Debra. Thanks JGM for so gently poking me in the eyes with this
one. I will go off and lick my wounds now ;-)
 
J

Jean-Guy Marcil

Greg was telling us:
Greg nous racontait que :
Uummm..., I guess I stepped in it with both feet <ssg> that's
"sheepish stupid grin"

I suppose my only saving grace was like Debra, I was rushing off to
something else as well. Fortunately my poor services rendered were
"free" and there is no client fee to return.

"oLinkHeader = False" was a complete brain puff attempt to abbreviate
the code.
Back to the drawing board on that.

Sorry Debra. Thanks JGM for so gently poking me in the eyes with this
one. I will go off and lick my wounds now ;-)

Sorry if it came across as "poking"...

All I meant to do was to point out some of the things that makes such an
endeavour complicated... just because this is a case of "been there, done
that" (and lost some hair in the process!)

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
G

Greg Maxey

JGM,

Absolutely no harm, no foul. That post was botched from A-z. I did not
feel that you were poking. Remember, the smiley wink is meant to keep it
all in fun.
 
D

Debra Ann

Well I just got back from another rush that I worked on from last Friday
until just now and I have exactly 24 hours to get this done for a 9:00
meeting tomorrow morning. I can see this is going to be a nightmare. I knew
it when they told me they wanted it done (and they want it done ... no ifs,
ands or buts) so this should be fun trying to trap all the errors.

Thanks to all of you for your options. At least my clients is my company
and I can just take the hits when it doesn't work.

Debra Ann
 
D

Debra Ann

Thank you all for your comments.

Greg, I was able to implement a good chunk of your code (using the less
cleaner version that you send before) and it worked great for my three areas
I was testing.

JGM, I took all of your other concerns an implemented them also except I am
struggling with the one on "If the cursor is exactly before the last
paragraph in the document, then the "\Page" bookmark generates an error. I
could probably test for the last paragraph before I selected the page but I'm
not sure on the code for that.

Any suggestions? I'm so close thanks to all of you.
 
G

Greg

Debra Ann,

Quick and dirty, you could add the following at the begining:

If Selection.End = ActiveDocument.Range.End - 1 Then
Selection.MoveEnd wdCharacter, -1
End If

There is then the possibility the that last paragraph mark is all that
exist on a page and you would therefore move up to the preceeding page.
What are the odds of that though?
 
J

Jean-Guy Marcil

Greg was telling us:
Greg nous racontait que :
Debra Ann,

Quick and dirty, you could add the following at the begining:

If Selection.End = ActiveDocument.Range.End - 1 Then
Selection.MoveEnd wdCharacter, -1
End If

There is then the possibility the that last paragraph mark is all that
exist on a page and you would therefore move up to the preceeding
page. What are the odds of that though?

Ever heard of Murphy's Law? ;-)

Not only that, it might be that the preceding page is a different section as
well!

To be thorough, the code should check for those situations...
Mine did, this is why it was so unbelievably long!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top