Use Find to move Insertion Point

N

Newfy-Lover

How do you move the Insertion Point using the Find method?

This code does not move the Insertion Point to where the find is.
Dim wdApp As New Word.Application
Dim doc As Word.Document
Dim WholeDocumentRange As Word.Range

doc = wdApp.Documents.Open(OutputFolder & "\" & FileName)

WholeDocumentRange = wdApp.ActiveDocument.Content
WholeDocumentRange.Find.ClearFormatting()
With WholeDocumentRange.Find
.Text = "Defect #" .Forward = True
.Wrap =
Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
WholeDocumentRange.Find.Execute()

<insert text here and it appears at the beginning of the
document and not at the Find location. The Find text is in the document.>

Thanks for your help.

Scott
 
J

Jay Freedman

Newfy-Lover said:
How do you move the Insertion Point using the Find method?

This code does not move the Insertion Point to where the find is.
Dim wdApp As New Word.Application
Dim doc As Word.Document
Dim WholeDocumentRange As Word.Range

doc = wdApp.Documents.Open(OutputFolder & "\" & FileName)

WholeDocumentRange = wdApp.ActiveDocument.Content
WholeDocumentRange.Find.ClearFormatting()
With WholeDocumentRange.Find
.Text = "Defect #" .Forward = True
.Wrap =
Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
WholeDocumentRange.Find.Execute()

<insert text here and it appears at the beginning of the
document and not at the Find location. The Find text is in the
document.>

Thanks for your help.

Scott

Hi Scott,

For what you're doing, you don't have to "move the insertion point". When
the Find.Execute succeeds, the Range object to which the .Find belongs (in
this case, WholeDocumentRange) is reassigned to cover the found text (but a
Range object is invisible unless you explicitly call its .Select method).

If you want to replace the found text with the new text, then you just
assign the new text to WholeDocumentRange.Text. However, I suspect that what
you really intend is to insert the new text _after_ the found text. To do
that, you collapse WholeDocumentRange to its endpoint (so it points to the
position immediately to the right of the "#" character) and then assign to
its .Text property:

...
End With
If WholeDocumentRange.Find.Execute() Then
WholeDocumentRange.Collapse
Microsoft.Office.Interop.Word.WdCollapseDirection.wdCollapseEnd
WholeDocumentRange.Text = <inserted text>
End If

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
S

StevenM

After: WholeDocumentRange.Find.Execute
Try: WholeDocumentRange.Select

If the find search is successful, the range object is redefined to include
the newly found text.

Steven Craig Miller
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top