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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word, VBA, Find and Replace

Status
Not open for further replies.

tareyj

Technical User
Joined
Jun 15, 2011
Messages
5
Location
US
Hi there, new member here. I searched with the search but am not able to find the answer I am looking for so hoping some one can give me a hand on this.
This is for word 2007
I am trying to create a button (thats the easy part) then link some VBA to go with it so when the button is clicked it will bring up the Find and Replace box. I searched the macros and know there is one (editfind) but I just cant figure out what the code is so it will execute the darn thing.

I am hoping this is easy that some one can just raddle the answer off to me which will be greatly appreciated. Thanks for your time and help.
 
Well, for one, even in Word 2007 hitting "CTRL+H" will bring up the find/replace dialog, no macro needed.

If you want to display the find/replace dialog from within VBA, you can do it with:
Code:
Sub PopupFind()
Word.Dialogs(wdDialogEditReplace).Show

That OK?

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Thanks for the quick responses but not 100% what I am looking for.

mintjulep
"The macro recorder is your friend."
I agree, but when I hit record macro and bring up the find window then stop recording it and make it into a button it does not pop up the find window.

MakeItSo
Correct about the CTRL +H and the +F .. +F is the one I want mostly to work but I want it to work as a button. I know word already has the button in the ribbon (which I am disabling) and the user can not hit CTRL anything. I only want to give them access to the button which will bring up the find window.
 
In that case either
Code:
Word.Dialogs(wdDialogEditReplace).Show
or
Code:
Word.Dialogs(wdDialogEditFind).Show
is your friend, depending on whether you want the find/replace or just the find dialog.

Put this code line in your macro behind the button and Bob's your uncle.

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Makeitso, Thanks that works.. sorta I failed to mention that I have the word document as a form so when its in the form protection it says run time error 4605 the method or property is not available because the current selection is protected area of the document. Know any work around? thanks for the help you have supplied so far.
 
Ah, now there's the rub.
Find won't work in protected forms.

You could try like this:
Code:
ActiveDocument.Unprotect
Word.Dialogs(wdDialogEditFind).Show
DoEvents
ActiveDocument.Protect

I doubt whether it will work this way though.
I'd say: add a User form to your macro, use THAT as your own custom Find dialog (a simple textbox and a "Search" button will do), add the "Unprotect / Protect" lines to your search button. Unprotect to execute the search, Protect again when found.

Good luck!
MakeItSo

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top