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



Comments

  • Find a specific "string"

    Posted by Legacy on 02/15/2004 12:00am

    Originally posted by: dav f

    Hi All
    What I need to change here ? Or ,
    How can I Do that the Find Dialog apear already with Constant "string" ?

    Reply
  • Bug fixes

    Posted by Legacy on 05/03/2002 12:00am

    Originally posted by: Ivan Krivyakov

    I found two bugs:
    
    

    1. If there is a non-text document open (e.g., resource), the macro fails.

    2. If replacement text contains search pattern again (e.g. Test -> m_Test), the macro will enter infinite loop.

    These two bugs are fixed in the code below:

    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
    if (doc.Type = "Text") then
    while Doc.Selection.FindText(FindText,MatchCase)
    Doc.Selection=ReplaceText
    'move to the right of replaced text, so that
    'it will not be searched again
    Doc.Selection.CharRight dsMove, Len(ReplaceText)
    wend
    end if
    next
    end if
    end if
    End Sub

    Reply
  • Little problem with this macro

    Posted by Legacy on 04/27/2002 12:00am

    Originally posted by: Hugo

    Thank you for this really usefull macro! Actually it gave me an introduction to using macro-scripts in Visual C.

    However I found one small problem, when you replace a word for something that also uses that same word, for example you try to replace "new" by "new_allocator", the macro recursivly replaces the text it just replaced, endlessly looping on the first occurance and only stopping when manually stopped. Is there a fix for this problem?

    Cheers.

    Reply
  • works, but...

    Posted by Legacy on 03/13/2002 12:00am

    Originally posted by: J Palmer

    This global macro seems to work ok but ends by displaying an error message:
    'Line 10 doesn't support this property or method: 'Doc.Selection'

    Reply
  • Replacement confirmation added

    Posted by Legacy on 07/31/2000 12:00am

    Originally posted by: Ilic Ferretti

    Hi!
    thank you for your macro, it's no doubt useful - one
    wonders why it isn't directly implemented in DEV Studio...

    As a Global Search and Replace operation can be cause of
    much pain, to which is then difficult to make up, I thought
    it could be nice to have possibility to confirm or skip
    operation for each occurrence of the string in question.
    That's what you should get with slight modifications I
    added. I'm not an expert in VB (and I hope I'll never need
    to be ! ;-) ). But it seems to work...

    bye
    Ilic


    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
    GoOn = 1
    if len(FindText) <> 0 then
    ReplaceText=InputBox("Replace all occurrences of '"+ Findtext + _
    "' with:", "Global Search and Replace", "<Remove Occurences of the Selection>")
    Message = "Replace this instance of '" + _
    FindText + "' with '" + ReplaceText + "' ?"
    if len(ReplaceText) <> 0 Then
    if ReplaceText = "<Remove Occurences of the Selection>" Then
    ReplaceText = ""
    Message = "Remove this instance of '" + FindText + "' ?"
    Else
    Message = "Replace this instance of '" + FindText + "' with '" + ReplaceText + "' ?"
    end if

    for each Doc in Application.Documents
    Documents.Open Doc.FullName, "Text"
    FirstFoundLine = -1
    GoOn = 1
    Doc.Selection.StartOfDocument
    while (Doc.Selection.FindText(FindText,MatchCase)) And (GoOn = 1)
    if FirstFoundLine = -1 Then
    FirstFoundLine = Doc.Selection.CurrentLine
    FirstFoundColumn = Doc.Selection.CurrentColumn
    Else
    If (Doc.Selection.CurrentLine = FirstFoundLine) And (Doc.Selection.CurrentColumn = FirstFoundColumn) Then
    GoOn = -1
    End If
    End If

    If GoOn = 1 Then
    RetVal = MsgBox(Message,vbYesNoCancel)
    if RetVal = vbYes Then
    Doc. Selection = ReplaceText
    ElseIf RetVal = vbCancel Then
    GoOn = 0
    End If
    End If
    wend
    if (GoOn = 0) Then
    Exit for
    End If
    next
    end if
    end if
    End Sub

    Reply
  • There is an Add-In also

    Posted by Legacy on 03/17/1999 12:00am

    Originally posted by: Marius Gheorghescu

    Quite an useful macro!

    There is also an Add-In called ReplaceAll, delivered as
    sample in MSDN/MSVC 6. It may be a litle bit handy...

    Reply
  • Global find and replace macro

    Posted by Legacy on 03/01/1999 12:00am

    Originally posted by: Stephen Ford

    This sort of macro can be very useful. I tend to use macros that interact more directly with the page layout, for example insert the body of an if() statement with the company standard comment layout starting at the current indentation point by pressing one key.

    I'm still figuring out the VB syntax so can only offer EMACS Lisp functions from the days of Prime Computers' implementation of EMACS!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Businesses are moving more and more of their customer transactions to the web. Security is understandably a top concern as online transactions increase, so it is important to make sure your electronic signature provider meets the highest security standards. That means more than simply passing a security audit or obtaining a certification. This white paper provides recommendations for taking a broader view of e-signature security, and answers key questions that help identify the security requirements against …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds