# Word Macro - If statement to search for text



## Loin75 (Aug 9, 2010)

Hi there,

I am trying to create a Word Macro that will search for a specific word in the document, so that I can perform certain tasks based on the word found.

There are 6 words that will be searched for, and only 1 will ever be present. (look for the first, if not, look for the 2nd, if not....etc)

When the word is found, I will be adding quick parts, formatting text, etc - but that part I can sort... It is the IF statement I am strugglign with.

Any help appreciated.

Thanks in advance..


----------



## macropod (Apr 11, 2008)

Hi Loin75,

You could use something along the following lines:

```
Sub FindMyWords()
Application.ScreenUpdating = False
Dim strWords As String, i, j As Integer
strWords = "quick,brown,fox,jumps,over,the,lazy,dog"
j = 0
For i = 0 To UBound(Split(strWords, ","))
  With ActiveDocument.Content
    With .Find
      .Text = Split(strWords, ",")(i)
      .Wrap = wdFindStop
      .Format = False
      .MatchCase = False
      .MatchWholeWord = True
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
      .Execute
    End With
    Do While .Find.Found
      j = j + 1
      .InsertBefore "["
      .InsertAfter "]"
      .Collapse wdCollapseEnd
      .Find.Execute
    Loop
  End With
Next i
MsgBox j & " instances found."
Application.ScreenUpdating = True
End Sub
```
If the treatment of each 'find' word differs, you could add the necessary logic within the Do While loop.


----------



## Loin75 (Aug 9, 2010)

Thats good work Macropod. This will work well...

Thanks ever so much...


----------

