• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 594
  • Last Modified:

how to write a word vba to found if is there a specific word in a file

Hi

How i write in word vba that if a specific word exist in this document then it's will run macro1 but it the word not exist in the file it's will run macro2

bill
0
bill201
Asked:
bill201
  • 2
1 Solution
 
Chris BottomleyCommented:
For example:

Sub sub1()

End Sub

Sub sub2()

End Sub


Sub main()
Dim rng As Range

    Set rng = ActiveDocument.Range
    With rng.Find
        .Text = "fred"
        .Execute
    End With
    If rng.Find.Found Then
        sub1
    Else
        sub2
    End If
    
End Sub

Open in new window


Chris
0
 
Peter KipropCommented:
try the below

Sub checkword()
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "wordtosearch"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
   
    If Selection.Find.Execute Then
        'execute macro 1
    Else
        'execute macro 2
    End If
End Sub
0
 
bill201Author Commented:
thanks alot
0
 
bill201Author Commented:
Pthepebble:
Oops sorry a second  after I approved the 500 points for chris_bottomley I saw your comment, it adds more flexibility in the code, so many thanks to you, and again I'm sorry.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now