Results 1 to 4 of 4
  1. #1
    5 Star Lounger Lugh's Avatar
    Join Date
    Jun 2010
    Location
    Indy
    Posts
    828
    Thanks
    209
    Thanked 106 Times in 93 Posts

    Passing Arguments

    I use a 'FindReplaceEngine' code I found over a decade ago to do many simple F/Rs in Word.
    Code:
    Sub FindReplaceEngine(MyFindText As String, MyReplacementText As String, _
      Optional MyMatchCase As Boolean = False, Optional MyMatchWildcards _
      As Boolean = False, Optional MyFormat As Boolean = False)
    'This is the replacement engine--pass named arguments to this for _
    'each individual Find-Replace operation. Can omit Optionals unless _
    'need to change them _
    'This will infinite-loop unless Find disappears when Replaced
    Selection.HomeKey Unit:=wdStory
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Wrap = wdFindContinue
        .MatchWholeWord = False
        .Forward = True
        .Text = MyFindText
        .Replacement.Text = MyReplacementText
        .MatchCase = MyMatchCase
        .MatchWildcards = MyMatchWildcards
        .Format = MyFormat
        Do While .Execute
            'Keep going until nothing found
            .Execute Replace:=wdReplaceAll
        Loop
        'Free up some memory
        ActiveDocument.UndoClear
    End With
    End Sub
    Works great for the many simple F/Rs I throw at it. Now I want to pass one of the optional arguments to it to make a F/R operation case sensitive.
    Code:
    Sub SalutationsFix()
    'Add periods after Mr, Mrs, Ms, Dr
    With MyMatchCase = True
    ...
        Call FindReplaceEngine(" Ms ", " Ms. ")
        Call FindReplaceEngine("^pMs ", "^pMs. ")
    ...
    End With
    End Sub
    [the ... are more similar lines]
    The "With" approach didn't work, " ms " got changed to " Ms. ", when it should have been ignored.

    Code:
    Sub SalutationsFix()
    'Add periods after Mr, Mrs, Ms, Dr
    ...
        MyMatchCase = True
        Call FindReplaceEngine(" Ms ", " Ms. ")
        MyMatchCase = True
        Call FindReplaceEngine("^pMs ", "^pMs. ")
    ...
    End Sub
    Above didn't work either, " ms " got changed to " Ms. "
    Sticking a dot before, ie
    Code:
        .MyMatchCase = True
    produced an error, as did the line
    Code:
        Call FindReplaceEngine(" Ms ", " Ms. ", "MyMatchCase = True")
    *white flag*

    What is the secret handshake?
    Lugh.
    ~
    Dell Alienware Aurora R6 (new 2017)
    Windows 10 Home x64 1703; Office 365 x32
    GeForce GTX 1060; 16GB DDR4 2400
    256G SSD, 1TB HD

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,901
    Thanks
    420
    Thanked 1,586 Times in 1,434 Posts
    Lugh,

    You can call the function positionally or by name:

    Positionally:
    Code:
    FindReplaceEngine " Ms ", " Ms. ",True
    By Name:
    Code:
    FindReplaceEngine MyFindText:=" Ms ", MyMatchCase:=True, MyReplacementText:=" Ms. "
    Note: The Call statement is optional.

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. The Following User Says Thank You to RetiredGeek For This Useful Post:

    Lugh (2016-08-01)

  4. #3
    5 Star Lounger Lugh's Avatar
    Join Date
    Jun 2010
    Location
    Indy
    Posts
    828
    Thanks
    209
    Thanked 106 Times in 93 Posts
    Thanks RG, got the positional working without the Call.

    I assume the True works as is because MyMatchCase is the first argument looked at by the Engine. So if I wanted to use MyFormat [the third argument] instead, I'd use sth like
    Code:
    FindReplaceEngine " Ms ", " Ms. ", ,,True
    Lugh.
    ~
    Dell Alienware Aurora R6 (new 2017)
    Windows 10 Home x64 1703; Office 365 x32
    GeForce GTX 1060; 16GB DDR4 2400
    256G SSD, 1TB HD

  5. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,901
    Thanks
    420
    Thanked 1,586 Times in 1,434 Posts
    Lugh,

    Bingo! However, I'd recommend using the named argument method as it makes it much easier to see what your code is doing. Six months down the road you'll be glad you did the extra typing!

    HTH
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •