Global find and replace macro
Posted
by Filip Dossche
on January 26th, 1999
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:00amOriginally posted by: dav f
Hi All
ReplyWhat I need to change here ? Or ,
How can I Do that the Find Dialog apear already with Constant "string" ?
Bug fixes
Posted by Legacy on 05/03/2002 12:00amOriginally posted by: Ivan Krivyakov
ReplyLittle problem with this macro
Posted by Legacy on 04/27/2002 12:00amOriginally 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:00amOriginally posted by: J Palmer
This global macro seems to work ok but ends by displaying an error message:
Reply'Line 10 doesn't support this property or method: 'Doc.Selection'
Replacement confirmation added
Posted by Legacy on 07/31/2000 12:00amOriginally 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
ReplyThere is an Add-In also
Posted by Legacy on 03/17/1999 12:00amOriginally posted by: Marius Gheorghescu
Quite an useful macro!
There is also an Add-In called ReplaceAll, delivered as
Replysample in MSDN/MSVC 6. It may be a litle bit handy...
Global find and replace macro
Posted by Legacy on 03/01/1999 12:00amOriginally 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