Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2005

Tip: Looking for answers? Try searching our database.

Test for End of Section

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Debra Ann - 27 Oct 2005 18:30 GMT
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!!!

Signature

Debra Ann

Doug Robbins - Word MVP - 27 Oct 2005 19:52 GMT
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

Signature

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

>I can't tell all of you how much help you have been to me in learning how
>to
[quoted text clipped - 13 lines]
>
> Thanks again!!!
Debra Ann - 27 Oct 2005 20:25 GMT
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

Signature

Debra Ann

> If Selection.Type = wdSelectionIP And Selection.End =
> Selection.Sections(1).Range.End - 1 Then
[quoted text clipped - 20 lines]
> >
> > Thanks again!!!
Greg - 27 Oct 2005 21:35 GMT
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
Tony Jollans - 27 Oct 2005 21:38 GMT
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

--
Enjoy,
Tony

> 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
[quoted text clipped - 64 lines]
> > > --
> > > Debra Ann
Debra Ann - 28 Oct 2005 14:00 GMT
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.

Signature

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
[quoted text clipped - 50 lines]
> > >
> > > Thanks again!!!
Greg - 28 Oct 2005 15:20 GMT
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
Greg - 28 Oct 2005 15:34 GMT
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
Debra Ann - 28 Oct 2005 16:15 GMT
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.

Signature

Debra Ann

> Or a little cleaner:
>
[quoted text clipped - 36 lines]
> End With
> End Sub
Debra Ann - 01 Nov 2005 16:09 GMT
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
Jean-Guy Marcil - 28 Oct 2005 16:43 GMT
Greg was telling us:
Greg nous racontait que :

> Or a little cleaner:
>
[quoted text clipped - 36 lines]
> 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..."

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org 

Greg - 28 Oct 2005 17:16 GMT
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 ;-)
Jean-Guy Marcil - 28 Oct 2005 21:09 GMT
Greg was telling us:
Greg nous racontait que :

> Uummm..., I guess I stepped in it with both feet <ssg> that's
> "sheepish stupid grin"
[quoted text clipped - 9 lines]
> 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!)

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org 

Greg Maxey - 28 Oct 2005 23:42 GMT
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.

Signature

Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

> Greg was telling us:
> Greg nous racontait que :
[quoted text clipped - 18 lines]
> an endeavour complicated... just because this is a case of "been
> there, done that" (and lost some hair in the process!)
Debra Ann - 01 Nov 2005 18:10 GMT
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.

---
Debra Ann
Greg - 01 Nov 2005 18:33 GMT
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?
Debra Ann - 01 Nov 2005 19:46 GMT
Worked perfect. Thanks.
Signature

Debra Ann

> Debra Ann,
>
[quoted text clipped - 7 lines]
> exist on a page and you would therefore move up to the preceeding page.
>  What are the odds of that though?
Jean-Guy Marcil - 02 Nov 2005 23:20 GMT
Greg was telling us:
Greg nous racontait que :

> Debra Ann,
>
[quoted text clipped - 7 lines]
> 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!

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org 

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.