Find & Replace Sample of multiple operations.
Posted: Thu Feb 07, 2008 6:52 pm
At the request of lapsedLibrarian I have created a macro to do multiple find & replace operation at one time. It is heavily commented and meant to provide you with a way to customize it for your own needs.
Please note the USER VARIABLE "AskMe" near the beginning of the file which controls whether you are asked about doing each of the operations.
Also note that my definition of the AskArray extends over several lines by using "_" to continue the definition to the next line. "_" must be the last character on the line and if is followed inadvertently by a space you will get an error.
Please note the USER VARIABLE "AskMe" near the beginning of the file which controls whether you are asked about doing each of the operations.
Also note that my definition of the AskArray extends over several lines by using "_" to continue the definition to the next line. "_" must be the last character on the line and if is followed inadvertently by a space you will get an error.
Code: Select all
Option Explicit 'You must Dim all variables.
Sub CleanUp
Dim AskMe as Boolean
'>>>>>>>>>>>>USER VARARBLE<<<<<<<<<<<
AskMe = True 'Change to False to avoid the query for each find & replace.
'>>>>>>>>>>END USER VARIABLE<<<<
Dim oDoc,AskArray,SearchArray,ReplaceAray,FandR,c,iAns
'Array contents:
AskArray = Array("Replace multiple spaces with one space?",_
"Delete spaces before paragraph breaks?",_
"Delete spaces before tabs?",_
"Delete spaces after tabs?")
REM Place in quotes what you would put in the Search/Replace boxes
REM in the Find & Replace dialog without the quotes.
SearchArray = Array(" *"," *$"," *\t","\t *")
ReplaceAray = Array(" " ,"" ,"\t" ,"\t" )
oDoc = ThisComponent 'Get the current active document.
FandR = oDoc.createReplaceDescriptor 'Set up find & replace.
FandR.searchRegularExpression = True 'Make it recognise regular expressions.
For c = 0 to uBound(SearchArray) 'Loop through the arrays.
If AskMe then 'If AskMe is True then ask.
iAns = MsgBox (AskArray(c),3)'See Help in the Basic IDE for the various
'buttons available in MsgBox and the integers they return.
If iAns = 2 then End 'Cancel button clicked so quit.
EndIf
IF iAns = 6 or AskMe = False then 'Yes button clicked, or assumed clicked,
'so do the find & replace.
FandR.setSearchString(SearchArray(c))
FandR.setReplaceString(ReplaceAray(c))
oDoc.ReplaceAll(FandR)
EndIf
'The No button will simple fall through to here so there is no need to process it.
Next c
End Sub