Results 1 to 5 of 5
  1. #1
    2 Star Lounger
    Join Date
    Mar 2001
    Location
    Colorado, USA
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Getting Find to stop at the end of a Word document

    I'm using the following code to count the number of a certain type of field in Word 2007 documents.
    Code:
        Do
            ' Find "Section " followed by a cross reference field
            Selection.Find.ClearFormatting
            With Selection.Find
                .Text = "Section ^d REF"
                .Replacement.Text = ""
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
            End With
            Selection.Find.Execute
            If Selection.Find.Found = False Then Exit Do
            
            intFieldCount = intFieldCount + 1
        Loop
    The problem is that the find does not stop when it reaches the end of the Word document. It just keeps running until it hangs and Word stops responding. If I change the Find.wrap setting to wdFindStop, it stops after finding just one occurrence, which is also not what I want.

    Can anyone tell me how to step through the document once, stopping at the end?

    Thanks
    Bill Lugg

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    After each successful find, the found text is selected. To avoid finding it again, collapse the selection to its end. I haven't looked up the precise syntax but approximately:

    Selection.Collapse wdCollapseEnd

  3. #3
    2 Star Lounger
    Join Date
    Mar 2001
    Location
    Colorado, USA
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK, I tried that, but it still does not stop at the end of the document after the first pass. It just keeps cycling through the document repeatedly until it finally hangs. Frankly, it doesn't seem to work like the help for Find.Wrap seems to describe. The help file indicates that this parameter is used to tell find what to do when it reaches the end of the document, but my experience was certainly not that, with it stopping after finding only the first occurrence.

    Bill Lugg

  4. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Sorry, I should have read your code more carefully. I think the way you are looping is starting a brand new find operation on every pass rather than running a single find operation, and that's why wrap doesn't behave the way you expect.

    If I move the loop inside, I get two passes, which is better, but still incorrect. Therefore, I suggest you force a single pass by starting from the top of the document and not wrapping. Here's some sample code:

    Code:
        ' Store cursor position and go to top of document
        Dim rngTemp As Word.Range
        Set rngTemp = Selection.Range
        Selection.HomeKey wdStory
        ' Find "Section " followed by a cross reference field
        intFieldCount = 0
        With Selection.Find
            .ClearFormatting
            .Text = "Section ^d REF"
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindStop
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            While .Execute = True
                intFieldCount = intFieldCount + 1
                Selection.Collapse wdCollapseEnd
            Wend
        End With
        MsgBox intFieldCount
        ' Restore cursor position
        rngTemp.Select
        Set rngTemp = Nothing
    Note to anyone testing, since this snippet is a bit out of context: you need to toggle the display of field codes from results to codes in order for Find to find the fields.


    Additional Thought: the peculiar results I'm getting with wrap appear to be related to the field codes, and the way Word automatically selects entire field results when a portion is selected. This seems to be confusing the start and end points of the find. Someone with deep interest might want to test that theory.
    Last edited by jscher2000; 2011-09-29 at 12:37. Reason: Another thought.

  5. #5
    2 Star Lounger
    Join Date
    Mar 2001
    Location
    Colorado, USA
    Posts
    142
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That was the ticket. I chose to use the Do While construct instead of While as it gives me a bit more flexibility in the loop, but that was my problem.

    Thanks for the help. It's greatly appreciated.
    Bill Lugg

Posting Permissions

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