Solved

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

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
PaperPort has a feature called the "Send To Bar". It provides a convenient, drag-and-drop interface for using other installed software, such as Microsoft Office. However, this article shows that the latest Office 2016 apps (installed with an Office …
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: …
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 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

19 Experts available now in Live!

Get 1:1 Help Now