Results 1 to 9 of 9
  1. #1
    Lounger
    Join Date
    Sep 2002
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    MACRO HELP (Word 2003)

    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

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: MACRO HELP (Word 2003)

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

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: MACRO HELP (Word 2003)

    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?

  4. #4
    Lounger
    Join Date
    Sep 2002
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: MACRO HELP (Word 2003)

    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.

  5. #5
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: MACRO HELP (Word 2003)

    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.

  6. #6
    Lounger
    Join Date
    Sep 2002
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: MACRO HELP (Word 2003)

    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

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: MACRO HELP (Word 2003)

    > 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.

  8. #8
    Lounger
    Join Date
    Sep 2002
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: MACRO HELP (Word 2003)

    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.

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: MACRO HELP (Word 2003)

    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.

Posting Permissions

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