Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word 2010 Macro

Status
Not open for further replies.

Akribie

Technical User
Apr 14, 2003
711
0
0
GB
I'm OK with Excel VBA, but haven't tried Word VBA recently. Using Office 2010 Pro under Win7 x64.

I want to set up a macro to search a document for a specific (constant) word. I know how to do this manually and I know how to link a macro to a button on the Quick Access toolbar.

My problem is that when I try recording a Macro of the find process - even when using only keyboard inputs due to mouse limitations in Word VBA recording - I get a macro with no code in it.

I have enabled all macros and set Developer Macro settings to trust access to the VBA project object model, but that hasn't helped.

Two questions:

1. How can I record a Find macro that works?

2. What is the Word VBA for finding a string in the whole document?

Thanks for any help.


 
The new Navigation Pane is not, as far as I know, available in VBA, so nothing is recorded. To get the old Find Dialog use Home Tab > Editing Group > Find > Advanced Find, and the code you need will be recorded.

What to code depends on what you want to do when you have found your string - the simplest code which mimics the old Find would be:

Selection.Find.Execute "string"

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Thanks for that. CTL-F or the Find command from the Editing Group in Word 2010 brings up the new dialogue, which won't record into a macro. Replace brings up the old macro.

Your code seems to work fine, even if there is no selection - which is how I want to use it. Thank you.

Pity the new Find dialogue seems not to be accessible from VBA because I find the list of hits it now gives saves me wasting lots of time wading through a long document when there is nothing relevant for me in the hits.
 
An alternative approach is

Result = Selection.Find.HitHighlight("text2find", wdColorYellow, , False)

which highlights all instances in the document, but I have yet to get the new searchg pane to open programmatiucally.
 
I just did a bit of testing and you can open it with code (which I knew) and use it (which I hadn't realised) ..

Code:
Selection.Find.Text = "string to find"
CommandBars.ExecuteMso "NavigationPaneFind"


Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top