How to: Word Macros with User Input?
June 10, 2010 3:37 PM Subscribe
WordMacroFilter: I'm trying to set up a Word Macro using find and replace but that waits for user input...
I want to set up some Word Macros (Word 2007 and 2010) that make use of the find and replace, but that don't do a 'blind' replace all.
So for example, I want to be able to have a macro that does a find and replace for two spaces and replaces it with one... but I don't want to do a replace all, because sometimes the documents in question have things that are positioned using spaces rather than tables. (yeah, yeah, I know, that's another issue.) I want the option to be able to say yay or nay to each replace.
If I record a macro using the find and replace set up, it just quits after one find and replace. Hitting the macro button multiple times really isn't viable.
How do I set this up? VB code examples would be great. I'm thinking Loop? I don't know...
(I have a number of such find and replace requirements, so I'm looking to save time on the set up of these via macros).
I want to set up some Word Macros (Word 2007 and 2010) that make use of the find and replace, but that don't do a 'blind' replace all.
So for example, I want to be able to have a macro that does a find and replace for two spaces and replaces it with one... but I don't want to do a replace all, because sometimes the documents in question have things that are positioned using spaces rather than tables. (yeah, yeah, I know, that's another issue.) I want the option to be able to say yay or nay to each replace.
If I record a macro using the find and replace set up, it just quits after one find and replace. Hitting the macro button multiple times really isn't viable.
How do I set this up? VB code examples would be great. I'm thinking Loop? I don't know...
(I have a number of such find and replace requirements, so I'm looking to save time on the set up of these via macros).
You could try something like below. It's a bit icky using SendKeys but it will work.
I've done it as separate procedures so that you can reuse the main procedure for different string combinations.
Otherwise I would look at creating your own form, displaying that and navigating through the document using that. If you take that approach you could opening up the form modelessly (Form1.Show vbModeless) so you could still scroll up and down if you wanted to on the active document.
posted by NailsTheCat at 4:58 PM on June 10, 2010
Sub FindAndReplaceSpaces()
FindAndReplace " ", " "
End Sub
Sub FindAndReplace(strToFind As String, strToReplaceWith As String)
frmSelect.Show (vbModal)
Dim rtn As Variant
With Dialogs(wdDialogEditReplace)
SendKeys strToFind
SendKeys "{tab}"
SendKeys strToReplaceWith
SendKeys "%r"
SendKeys "%r"
rtn = .Display
End With
End Sub
I've done it as separate procedures so that you can reuse the main procedure for different string combinations.
Otherwise I would look at creating your own form, displaying that and navigating through the document using that. If you take that approach you could opening up the form modelessly (Form1.Show vbModeless) so you could still scroll up and down if you wanted to on the active document.
posted by NailsTheCat at 4:58 PM on June 10, 2010
Oops. Remove that line "frmSelect.Show (vbModal)". That was just me checking the constants for modal / modeless.
posted by NailsTheCat at 5:04 PM on June 10, 2010
posted by NailsTheCat at 5:04 PM on June 10, 2010
What you want is to put your search & replace in an endless loop, and have it break (using "Exit Do") when (a) there is nothing found, or (b) the user says so. To get the user's feedback, use the MsgBox function, which shows a Windows-standard message box.
In your Visual Basic (Alt-F11 to open) search for help on MsgBox. Basically, in the loop you want something like:
posted by Simon Barclay at 5:26 PM on June 10, 2010
In your Visual Basic (Alt-F11 to open) search for help on MsgBox. Basically, in the loop you want something like:
Do '... insert search here... '... add code so that if string isn't found, then exit do or exit sub Select Case MsgBox("Do you want to replace this occurrence?", _ vbYesNoCancel+vbInformation,"Search/Replace") Case vbYes: '...insert code to replace... Case vbNo: ' keep this empty Case vbCancel: Exit Do ' or use Exit Sub instead, if that's better LoopI haven't tried this out, but that's the basic structure you're looking for (I think).
posted by Simon Barclay at 5:26 PM on June 10, 2010
This thread is closed to new comments.
posted by gubenuj at 4:35 PM on June 10, 2010