With.Selection.Find & strings

L

Lucas

I need to search for the second occurrence in my document
of whatever the value of the first line happens to be.

I'm unsure how to get With.Selection.Find.Text to use a
value from a string that I've set earlier in the macro.
Is this even possible?

Thanks in advance.

Sub Whatever()
Dim Company As String
Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Company = Selection
MsgBox Company, vbOKOnly, "blah"
Selection.EndKey Unit:=wdLine
Selection.Find.ClearFormatting
With Selection.Find
.Text = Company
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
End Sub
 
S

Shauna Kelly

Hi Lucas

Try something like the following:

Sub FindSecondOccurreceOfTheTextOfTheFirstLine()

Dim strCompany As String
Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
strCompany = Selection.Range.Text

Selection.Collapse Direction:=wdCollapseEnd

Selection.Find.ClearFormatting
With Selection.Find
.Text = strCompany 'Use the variable name here
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop 'Don't use wdFindContinue or
'it will wrap and find the first line
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute 'Perform the search

If .Found Then
MsgBox "We found the text"
Else
MsgBox "We didn't find the text"
End If
End With
End Sub


Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 
Top