Solved

Word 2007 Macro - Find text in document

Posted on 2007-04-03
2
1,670 Views
Last Modified: 2007-11-27
I have some text that I want to find all instances of in my document, what is the synatx of the code?  I attempted to discover it in a macro, but it uses the execute to determine the number and I cant seem to get any further, please help!
0
Comment
Question by:jessnjeff
  • 2
2 Comments
 
LVL 7

Expert Comment

by:gbahri
ID: 18848436
Hi  jessnjeff,

You can use "Do while .... loop" to achieve this, here is a snippet for the same. Let's say, you wish to find "jessnjeff":

========================================================================

Sub FindAllInstances()

Selection.HomeKey Unit:=wdStory
  With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindStop ' find till eof
    .MatchWildcards = False
    .Text = "jessnjeff"
    Do While .Execute = True
          MsgBox Selection.Text ' Will show text found in document (instances of jessnjeff one by one.)
    Loop
       
   End With
End Sub

========================================================================

Hope, this serves your purpose,

thanks,

GBahri


0
 
LVL 7

Accepted Solution

by:
gbahri earned 125 total points
ID: 18972045
Any update?
0

Featured Post

A Knowledge Base That Stays Up-to-Date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

743 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

12 Experts available now in Live!

Get 1:1 Help Now