#1
|
|||
|
|||
Function in a Macro used to Find & Replace
I have a Macro that I'm trying to make more efficient and also be able to share & update a little easier. My attempt was to create a function that could be called for upwards of 25 corrections that change over time and should be easy to update with just one line of code calling the function.
Can anyone deduce what I am trying to do and what I'm writing incorrectly? Code:
Private Function Simple_FindReplace(docFind As Document, strFind As String, _ strReplace As String) With docFind.Range.Find .ClearFormatting .Replacement.ClearFormatting .Text = strFind .Replacement.Text = strReplace .Execute Replace:=wdReplaceAll End With End Function Sub DocOps_SPD_Rewrites() With Document.Range.Find 'SPD Changes - Updated 2.14.2019 Call Simple_FindReplace("BEFORE text", "AFTER text") Call Simple_FindReplace("BEFORE test", "AFTER test") Call Simple_FindReplace("BEFORE tent", "AFTER tent") End With End Sub |
#2
|
||||
|
||||
I can't see why you'd need a function, when what you're after can be done via a loop in the main sub. See, for example, in order of increasing complexity:
https://www.msofficeforums.com/word-...html#post55215 https://www.msofficeforums.com/word-...html#post93792 https://www.msofficeforums.com/word-...html#post72330 https://www.msofficeforums.com/word-...html#post93796
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
||||
|
||||
If you stay with the function, the reason it is failing is that you are only passing in two variables but the function requires three variables.
Code:
Sub DocOps_SPD_Rewrites() Call Simple_FindReplace(ActiveDocument, "BEFORE text", "AFTER text") Call Simple_FindReplace(ActiveDocument, "BEFORE test", "AFTER test") Call Simple_FindReplace(ActiveDocument, "BEFORE tent", "AFTER tent") End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#4
|
|||
|
|||
Thank you, I've got it setup this way simply to make updating easier, so that each Find/Replace is on a single line. This works flawlessly now, I've loaded to my Normal Template and can apply to Active Document!
|
|