Selection.Find.Execute Problem

N

neo2cents

I am pulling a number (eg, 3.2.1.1.1.2) from an Excel doct and then
trying to locate that position in a Word(2003) doct., and insert some
text below that line. The sub works properly the first time through,
but fails after that. The cursor ends up at the top of the doct but
the Find.execute does not happen after that. Can someone help me with
this?
Thanks

Sub Locate_in_Doct()
'
' Locate_in_Doct Macro
'
'
ActiveDocument.Range(0, 0).Select
Selection.Find.ClearFormatting
With Selection.Find
.Text = sExcelTextJ ' Example "3.2.1.1.1.2" (SExcelTextJ
is a Public String)
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.Collapse Direction:=wdCollapseEnd
If Selection.IPAtEndOfLine = False Then
Selection.EndKey Unit:=wdLine, Extend:=wdMove
End If

Selection.TypeText Text:=vbCrLf & "Locate Here " & vbCrLf
End Sub
 
G

Greg Maxey

You have to repeat .Execute while the item is found:

Sub Locate_in_Doct()
Dim sExcelTextJ
sExcelTextJ = "3.2.1.1.1.2"
ActiveDocument.Range(0, 0).Select
Selection.Find.ClearFormatting
With Selection.Find
.Text = sExcelTextJ
While .Execute
Selection.Collapse Direction:=wdCollapseEnd
If Selection.IPAtEndOfLine = False Then
Selection.EndKey Unit:=wdLine, Extend:=wdMove
End If
Selection.TypeText Text:=vbCrLf & "Locate Here " & vbCrLf
Wend
End With
End Sub
 
N

neo2cents

I tried this but on the call to the sub (new sExcelTextJ value) the
While statement does not execute. I am trying to find out how to reset
the selection properties, but I haven't found it yet. Or is this not
the problem?
 
N

neo2cents

The problem I had was not with the selection.find.
When I extracted the string from Excel, I had leading spaces or blanks
that did not
show up in the debugger window. Adding trim$( ) to the Excel string
solved my problem.
Thanks,
 

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

Similar Threads


Top