View Single Post
Old 06-12-2009, 02:43 PM
Bird_FAT's Avatar
Bird_FAT Bird_FAT is offline Office 2007
Join Date: Apr 2009
Location: South East
Posts: 271
Bird_FAT is on a distinguished road

In the following example, I am running a Find and Replace string for the word 'dog', Then I am using If statements to choose what to do if I find the word, or not. (read the green tags for info on each section)

Sub Find_Then_IF_Macro()
' Example by Bird
' This first section is the Find string - the only really important
' line is the one in RED, as this is what we are searching for

    Selection.HomeKey Unit:=wdStory
    With Selection.Find
        .Text = "dog"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With

' This next part is where we decide on the action to be taken if
' the text we are looking for IS present.
' I just chose to move the cursor 5 places to the right!
    If Selection.Find.Execute = True Then
        Selection.MoveRight Unit:=wdCharacter, Count:=5

' This next section is what we want to happen if the text we are
' looking for is NOT found; in this case, I have stopped the code
' by using Exit Sub (i.e. STOP)
        If Selection.Find.Execute = False Then
            Exit Sub

' Now, to finish off, we have to add an End If statement for
' every If that we had.
' NOTE: If you had values other than 'Yes/No' or 'True/False'
' then you could have more Else, If statements.
        End If
    End If
End Sub
Hope that helps - I'll be monitoring this post if you have any problems!
Reply With Quote