Leprechaun
asked on
Check if a string exists in a text file.
I have to perform an action in VBA for Word depending on whether a given phrase appears in a text file.
How can I make an IF THEN statement depend on whether or not the string appears in the document?
Thanks.
Leprechaun
How can I make an IF THEN statement depend on whether or not the string appears in the document?
Thanks.
Leprechaun
Leprechaun,
Here is some Word VBA code that checks if the active document contains the text "Corned Beef".
Private Sub Command1_Click()
Dim doc As Word.Document
Dim f As Boolean
Dim rng As Word.Range
Set doc = AvtiveDocument
Set rng = doc.Range(0, 0)
With rng.Find
.Text = "Corned Beef"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
f = rng.Find.Execute
If f = True Then
Msgbox "Found it!"
Else
Msgbox "No beef..."
End
End Sub
Ture Magnusson
Karlstad, Sweden
Here is some Word VBA code that checks if the active document contains the text "Corned Beef".
Private Sub Command1_Click()
Dim doc As Word.Document
Dim f As Boolean
Dim rng As Word.Range
Set doc = AvtiveDocument
Set rng = doc.Range(0, 0)
With rng.Find
.Text = "Corned Beef"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
f = rng.Find.Execute
If f = True Then
Msgbox "Found it!"
Else
Msgbox "No beef..."
End
End Sub
Ture Magnusson
Karlstad, Sweden
ASKER
Thanks for your response, Mark.
Yeah, I do need help "reading the contents of the text file into LongTextString".
Thanks in advance...
Leprechaun
Yeah, I do need help "reading the contents of the text file into LongTextString".
Thanks in advance...
Leprechaun
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Now you should use UCASE on both those clauses to make it more robust. If you need help realding the contents of the text file into LongTextString let me know.
M