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
 

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