Results 1 to 4 of 4
  1. #1
    5 Star Lounger Lugh's Avatar
    Join Date
    Jun 2010
    Location
    Indy
    Posts
    619
    Thanks
    166
    Thanked 75 Times in 66 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.
    ~
    Windows 10 Pro x64 1607; Office 2016 (365 Home) x32; Win Defender, MBAM Pro

    ASRock H97 Anniversary; Xeon E3-1231V3 (like i7)
    Gigabyte GeForce GTX 970; 12GB Crucial DDR3 1600
    Logitech MX Master mouse; Roccat Isku kb

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 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
    619
    Thanks
    166
    Thanked 75 Times in 66 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.
    ~
    Windows 10 Pro x64 1607; Office 2016 (365 Home) x32; Win Defender, MBAM Pro

    ASRock H97 Anniversary; Xeon E3-1231V3 (like i7)
    Gigabyte GeForce GTX 970; 12GB Crucial DDR3 1600
    Logitech MX Master mouse; Roccat Isku kb

  5. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,433
    Thanks
    371
    Thanked 1,456 Times in 1,325 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
  •