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).
posted by Zinger to Computers & Internet (4 answers total) 1 user marked this as a favorite
 
Since no matter what, you want to visually inspect each occurrence, the only way I can think to do this is to set up two macros. The first, let's call it "Find-next" finds the two spaces, and then moves back to the previous character. If you want to replace, then you invoke the second, we'll call "Find and Replace", finds the one you just found and then replaces it and moves forward a character. If you don't want to replace, you just invoke your "Find-next" macro twice (because you are at the character before the occurrence) to find the next subsequent occurrence. If you set up shortcut keys, (e.g. ctrl-6 for Find-next and ctrl-7 for Find and Replace) you can probably pretty quickly move through your document (just as quickly as pressing "y" and "n") assuming you were able to make a loopy VB code.
posted by gubenuj at 4:35 PM on June 10, 2010


You could try something like below. It's a bit icky using SendKeys but it will work.


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


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:
     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
 
    Loop
I 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


« Older Should I drop Zyrtec?   |   Foods/resources for healthy weight gain? Newer »
This thread is closed to new comments.