Solved

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

Posted on 2013-05-10
4
587 Views
Last Modified: 2013-05-10
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
Comment
Question by:bill201
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 39154867
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
 
LVL 6

Expert Comment

by:Peter Kiprop
ID: 39154886
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
 

Author Closing Comment

by:bill201
ID: 39154889
thanks alot
0
 

Author Comment

by:bill201
ID: 39154897
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question