Link to home
Start Free TrialLog in
Avatar of Leprechaun
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
Avatar of mark2150
mark2150

IF INSTR(LongTextString, "What I'm Seaching for") > 0 THEN ...

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
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
Avatar of Leprechaun

ASKER

Thanks for your response, Mark.

Yeah, I do need help "reading the contents of the text file into LongTextString".

Thanks in advance...

Leprechaun
ASKER CERTIFIED SOLUTION
Avatar of eab111098
eab111098

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial