Solved

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

Posted on 2013-05-10
4
556 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
No matter the version of Windows you are using, you may have some problems with Windows Search running too slow or possibly not running at all. Before jumping into how you can solve this issue, just know there are many other viable alternative deskt…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now