BruceWayne
BruceWayne

Reputation: 23283

Using .Find won't continue, stays on same paragraph

I have a script that looks for some text, inputted by the user. The idea is to look through a document for this text, and when it's found, select the paragraph and ask the user if they want to add this paragraph to an Index.

For some reason, I can't get the script to move past the first selected paragraph. When I run it, and click "Yes" in the UserForm (equivalent of myForm.Tag = 2), it adds to the index, but then when the .Find looks for the next instance of the text, it selects the paragraph I just had highlighted. ...it doesn't continue.

Here's the code:

Sub find_Definitions()
Dim defText As String, findText$
Dim oRng    As Word.Range, rng As Word.Range
Dim myForm As frmAddDefinition
Set myForm = New frmAddDefinition

Dim addDefinition$, expandParagraph&
' expandParagraph = 1

Set oRng = ActiveDocument.Range
findText = InputBox("What text would you like to search for?")

With oRng.Find
    .Text = findText
    While .Execute

    Set rng = oRng.Paragraphs(1).Range
        rng.Select
        defText = oRng.Paragraphs(1).Range
        myForm.Show
        Select Case myForm.Tag
            Case 0 ' Expand the paragraph selection

                Do While CLng(expandParagraph) < 1
                    expandParagraph = InputBox("How many paragraphs to extend selection?")
                    If expandParagraph = 0 Then Exit Do
                Loop

                    rng.MoveEnd unit:=wdParagraph, Count:=expandParagraph
                    rng.Select
                    defText = rng

                ActiveDocument.Indexes.MarkEntry Range:=rng, entry:=defText, entryautotext:=defText
            Case 1 ' No, do not add to the index
                ' do nothing
            Case 2 ' Yes, add to index
                ActiveDocument.Indexes.MarkEntry Range:=rng, entry:=defText, entryautotext:=defText
            Case 3 ' Cancel, exit the sub
                MsgBox ("Exiting macro")
                GoTo lbl_Exit
        End Select
    Wend
End With

lbl_Exit:
Unload myForm
Set myForm = Nothing
End Sub

(FWIW, I'm pretty new to Word VBA, but very familiar with Excel VBA). Thanks for any ideas.

Note if I click "No" (equivalent of myForm.Tag = 1), then it does move on to the next instance. Hmm.

Upvotes: 0

Views: 264

Answers (1)

Cindy Meister
Cindy Meister

Reputation: 25663

Try adding rng.Collapse wdCollapseEnd before the "Case 1" line.

Explanation: When you use Find, it executes on the given Range or Selection.

If it's successful, that Range/Selection changes to include the "found" term. In this case, you in addition change the assignment again (expanding to include the paragraph).

When your code loops the current assignment to "Range" is used - in this case, Find looks only at the selected paragraph Range. So you need to reset the Range in order to have Find continue.

To be absolutely accurate, after Collapse you could also add:

rng.End = ActiveDocument.Content.End

Note: it's more correct to use ActiveDocument.Content than ActiveDocument.Range. ActiveDocument.Range is actually a method for creating a new Range by specifying the Start and End points, while ActiveDocument.Content returns the entire main story (body) of the document as a Range object. VBA doesn't care, it defaults the method to return the main story. Other programming languages (.NET, especially C#) don't work as intuitively with Word's object model, however. So it's a good habit to use what "always" works :-)

Upvotes: 1

Related Questions