Results 1 to 5 of 5
  1. #1
    2 Star Lounger
    Join Date
    Oct 2007
    Posts
    172
    Thanks
    0
    Thanked 2 Times in 1 Post

    GotoNext in a loop (Word 2003)

    Hi,

    I want to do a loop based on the Selection.GoToNext(wdGoToSection) method. Its certainly possible, but I am not sure what happens (or what is supposed to happen) when the end of the document is reached. It seems to go back to the beginning of the document. Does anyone know if this method returns anything you can check in a Do While loop to see if the end of the document has been reached?

    I would rather do this than the "Find.Execute" method looking for "^b" special format characters, which adds quite a few lines of extra code.

    cheers

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: GotoNext in a loop (Word 2003)

    You could use something like this:

    Dim lngStart As Long
    Dim lngPrevStart As Long
    Selection.HomeKey Unit:=wdStory
    lngStart = Selection.Start
    Do
    Selection.GoToNext wdGoToSection
    lngPrevStart = lngStart
    lngStart = Selection.Start
    Loop Until lngStart = lngPrevStart

    But depending on what you want to do you might also use something like this:

    Dim i As Integer
    For i = 1 To ActiveDocument.Sections.Count
    ' do something with ActiveDocument.Sections(i)
    Next i

    or

    Dim sec As Section
    For Each sec In ActiveDocument.Sections
    ' do something with sec
    Next sec

  3. #3
    2 Star Lounger
    Join Date
    Oct 2007
    Posts
    172
    Thanks
    0
    Thanked 2 Times in 1 Post

    Re: GotoNext in a loop (Word 2003)

    Hi, thanks.. i think the first suggestion is the one I am looking for.. Basically I am looping through all sections in the document and deleting the section breaks except for the first one. Can you think of a quick way of doing this?

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: GotoNext in a loop (Word 2003)

    Searching for ^b and replacing with "" is the most efficient method.
    Or this perhaps:

    Dim i As Integer
    Dim myRange As Range
    For i = ActiveDocument.Sections.Count - 1 To 2 Step -1
    Set myRange = ActiveDocument.Sections(i).Range
    myRange.Collapse wdCollapseEnd
    myRange.MoveEnd Unit:=wdCharacter, Count:=-1
    myRange.Delete
    Next i

  5. #5
    2 Star Lounger
    Join Date
    Oct 2007
    Posts
    172
    Thanks
    0
    Thanked 2 Times in 1 Post

    Re: GotoNext in a loop (Word 2003)

    ok thanks.. good to know i am not going mad :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •