Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determining if cursor is in last section of a doc?

Status
Not open for further replies.

Walipala

Technical User
Jan 5, 2007
21
US
Hi folks. Is there a VBA expression (returning a boolean maybe) that will determine if the cursor resides in the last section of a Word (2003) document? As far as usage goes, I'm looking for something roughly similar to:

If thisLocation.isInLastSection = True Then
Do stuff...
End If
 
Hi Gerry. (And thanks Tony!) It doesn't really matter where in the last section. What I want is a Do-While loop that marches though each section of a doc and performs a task. It then determines if the current section is the FINAL section and if so, a flag is switched that will cause the loop to end and stop the macro. It looks---or will look---something like this (with extraneous code removed):

Dim Again As Boolean
Again = True

Do While Again = True
Perform a task...

' If this is final section in document
If Selection.Sections(1).Index = ActiveDocument.Sections.Count Then
Again = False
Else
' Move to next section
Selection.GoTo What:=wdGoToSection, Which:=wdGoToNext
End If
Loop
 
Quick follow-up: I just ran the above macro on a Word doc that has about 30 section breaks. It worked great---did everything it was supposed to and quit when it finished. Thanks for the help.
 
You don't need to do that. There are a couple of ways to iterate collections ...
Code:
[blue]For i = 1 to ActiveDocument.Sections.Count
    [green]' Work with section(i) here[/green]
Next[/blue]
Code:
[blue]For Each Sect in ActiveDocument.Sections
    [green]' Work with Sect here[/green]
Next[/blue]

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

Professional Office Developers Association
 
Absolutely. Tony is right again.

Use a Section object, or iterate through the collection.

Try to avoid using Selection. Using Selection to move through the document is inefficient.

Gerry
My paintings and sculpture
 
Yep. I tried them both. Seems there's always an easier way. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top