Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
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 …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

820 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