Global find and replace macro

I have noticed someone wanting a global find and replace for Visual c++ developer studio 97. I needed that to so I made a couple of VBSCRIPT macros to do it. They are quite simple and possibly even somewhat crude but they do the job quite well. Maybe somebody has done this sort of thing before but here they are for what its worth.

To use the macros do the following:

  • open a document or documents
  • mark the text to replace ( usual stuff, pressing SHIFT or using mouse )
  • run the macro ( FindAndReplace for single file, GlobalFindAndReplace for global ).

Remarks:

  • In case no text is selected (marked) nothing happens.
  • "Global" find and replace is liimted to all open files (but Developer studio can really keep open a lot of them at the same time)

VBSCRIPT MACROS:


Sub GlobalFindAndReplace()
    'DESCRIPTION: Does a global find and replace in all open documents. 
    'Small, quick macro, but it can be useful.
    MatchCase=4
    FindText=ActiveDocument.Selection
    if len(FindText) <> 0 then
        ReplaceText=InputBox("Replace all occurrences of text: "+ vbCrLf + " => " + Findtext + vbCrLf +"with text :")
        if len(ReplaceText) <>0 then
            for each doc in Application.Documents
                while Doc.Selection.FindText(FindText,MatchCase)
                    Doc.Selection=ReplaceText
                wend
            next
        end if
    end if
End Sub

Sub FindAndReplace()
    'DESCRIPTION: Does a find and replace in the currently displayed open document
    'Small, quick macro, but it can be useful.
    MatchCase=4
    FindText=ActiveDocument.Selection
    if len(FindText) <> 0 then
        ReplaceText=InputBox("Replace all occurrences of text: "+ vbCrLf + " => " + Findtext + vbCrLf +"with text :")
        if len(ReplaceText) <>0 then
            while ActiveDocument.Selection.FindText(FindText,MatchCase)
                ActiveDocument.Selection=ReplaceText
            wend
        end if
    end if
End Sub