• MACRO HELP (Word 2003)

    Author
    Topic
    #430655

    The WOPR Normal Quotes button allows me to change smart quotes to regular quotes, and then smart apostrophes to regular apostrophes. Two Find and Replace operations one after another.

    I’d like to be able to create my own version where I could Find and Replace a series of terms one after another.

    Efforts to do this have not met with success because I apparently don’t understand how to tell the macro to wait while the first operation is performed before trying to perform the second operation.

    All help welcomed.

    Ian

    Viewing 2 reply threads
    Author
    Replies
    • #1006198

      Could you give a concrete example of what you would like to do?

    • #1006202

      Find and Replace generally is done in a serial fashion; I don’t think it’s possible to start a second one without completing the first one. Perhaps, though, some important step was omitted when you recorded or wrote the macro?

    • #1006223

      First operation: Find “three” Replace with “3”
      Second operation: Find “one hundred percent” Replace with “100%”
      Third operation: Find “shall not” Replace with “won’t”

      and so on.

      I’ve written individual macros for each of the operations. But that means manually performing them. What I want to be able to do is put them together serially so that the first Find/Replace (or macro) runs, then the second, and so on.

      • #1006228

        Here is an example:

        Sub ReplaceOne(strWhat As String, strWith As String, Optional blnWholeWord As Boolean)
        With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Format = False
        .MatchCase = False
        .MatchWholeWord = blnWholeWord
        .MatchWildcards = False
        If Selection.Type = wdSelectionIP Then
        .Wrap = wdFindContinue
        Else
        .Wrap = wdFindStop
        End If
        .Text = strWhat
        .Replacement.Text = strWith
        .Execute Replace:=wdReplaceAll
        End With
        End Sub

        Sub ReplaceMany()
        ReplaceOne “three”, “3”, True
        ReplaceOne “one hundred percent”, “100%”
        ReplaceOne “shall not”, “won’t”
        End Sub

        The ReplaceOne procedure is general, you specify a “find what” text and a “replace with” text, and optionally a True/False value to indicate whether you want to replace whole words only. If you omit the third argument, False is assumed. You can modify ReplaceOne if you want to add or change its functionality, otherwise leave it as it is.

        The ReplaceMany macro is the one to modify and to run. It calls ReplaceMany once for each find what/replace with pair. You can add/edit/remove lines as needed.

        • #1006231

          Hans,

          Thanks but I’m just not getting it as you’ve explained. Sorry.

          If I create a macro that’s just what you’ve written as Sub ReplaceMany, I get an error that no function is described.

          The only way I was able to get this to work was this but it’s hard to believe you meant to have to do this each time:

          Sub ReplaceMany()
          With Selection.Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Format = False
          .MatchCase = False
          .MatchWholeWord = blnWholeWord
          .MatchWildcards = False
          If Selection.Type = wdSelectionIP Then
          .Wrap = wdFindContinue
          Else
          .Wrap = wdFindStop
          End If
          .Text = “three”
          .Replacement.Text = “3”
          .Execute Replace:=wdReplaceAll
          End With
          With Selection.Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Format = False
          .MatchCase = False
          .MatchWholeWord = blnWholeWord
          .MatchWildcards = False
          If Selection.Type = wdSelectionIP Then
          .Wrap = wdFindContinue
          Else
          .Wrap = wdFindStop
          End If
          .Text = “one hundred percent”
          .Replacement.Text = “100%”
          .Execute Replace:=wdReplaceAll
          End With

          With Selection.Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Format = False
          .MatchCase = False
          .MatchWholeWord = blnWholeWord
          .MatchWildcards = False
          If Selection.Type = wdSelectionIP Then
          .Wrap = wdFindContinue
          Else
          .Wrap = wdFindStop
          End If
          .Text = “shall not”
          .Replacement.Text = “with”
          .Execute Replace:=wdReplaceAll
          End With
          End Sub

          • #1006233

            > it’s hard to believe you meant to have to do this each time

            Of course not. The idea was that you should copy both procedures (ReplaceOne and ReplaceMany) from my previous reply into a code module. You should then be able to run ReplaceMany.

            Note: ReplaceOne is not meant to be run on its own. It is not a macro since it has arguments.

            • #1006236

              What was I missing was putting it in its own code module. I had just added to a module that had a bunch of macros in it. But putting it in its own made it run perfectly. Many thanks.

            • #1006239

              In itself, it shouldn’t matter whether you added the procedures to an existing module or to a new one. But I’m glad you’ve got it working.

    Viewing 2 reply threads
    Reply To: MACRO HELP (Word 2003)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: