Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-14-2019, 09:09 AM
koehlerc14 koehlerc14 is offline Function in a Macro used to Find & Replace Windows 10 Function in a Macro used to Find & Replace Office 2016
Novice
Function in a Macro used to Find & Replace
 
Join Date: Feb 2019
Posts: 6
koehlerc14 is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 02-14-2019, 01:48 PM
macropod's Avatar
macropod macropod is offline Function in a Macro used to Find & Replace Windows 7 64bit Function in a Macro used to Find & Replace Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #3  
Old 02-14-2019, 03:04 PM
Guessed's Avatar
Guessed Guessed is online now Function in a Macro used to Find & Replace Windows 10 Function in a Macro used to Find & Replace Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 3,993
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

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
Reply With Quote
  #4  
Old 02-15-2019, 11:59 AM
koehlerc14 koehlerc14 is offline Function in a Macro used to Find & Replace Windows 10 Function in a Macro used to Find & Replace Office 2016
Novice
Function in a Macro used to Find & Replace
 
Join Date: Feb 2019
Posts: 6
koehlerc14 is on a distinguished road
Default

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!
Reply With Quote
Reply



Other Forums: Access Forums

All times are GMT -7. The time now is 10:27 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft