Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Feb 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Macro and Text Boxes

    I created a macro intended to do some post-merge cleanup on the final documents. It works properly in regular documents but won't find and replace my text when it appears in a text box. Does anyone know how to fix this? Below is the code for what I have now:

    Sub rcrstockcerts()
    '
    ' rcrstockcerts Macro
    '
    '
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = "* "
    .Replacement.Text = "*"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.Find.Execute Replace:=wdReplaceAll
    With Selection.Find
    .Text = "( "
    .Replacement.Text = "("
    .Forward = True
    .Wrap = wdFindContinue
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.Find.Execute Replace:=wdReplaceAll
    Selection.Find.Execute Replace:=wdReplaceAll
    End Sub

  2. #2
    2 Star Lounger
    Join Date
    Dec 2009
    Location
    Canada
    Posts
    122
    Thanks
    3
    Thanked 20 Times in 18 Posts
    This is a known limitation of Word's Find & Replace: it works when you use the dialog, but the recorded version of the same thing won't work. For a workaround and some sample code you could modify, refer to this Word MVP faq page. Note that the example uses the more efficient object model rather than the selection model you will get when you record a macro.

    From your code example, it looks like you are repeating the replace several times to eliminate all possible whitespace after the ( and * characters. You can accomplish the same thing by using the search pattern (^w to find a parentheses character followed by any amount of white space. This will find "( " or "( " but also "(" followed by a tab or any combination of spaces and tabs.

  3. #3
    Super Moderator
    Join Date
    May 2002
    Location
    Canberra, Australian Capital Territory, Australia
    Posts
    5,054
    Thanks
    2
    Thanked 417 Times in 346 Posts
    Try:
    Code:
    Sub rcrstockcerts()
    Application.ScreenUpdating = False
    Dim Rng As Range
    With ActiveDocument
      For Each Rng In .StoryRanges
        With Rng.Find
          .ClearFormatting
          .Replacement.ClearFormatting
          .Text = "([\*\(])[ ]{1,}"
          .Replacement.Text = "\1"
          .Forward = True
          .Wrap = wdFindContinue
          .Format = False
          .MatchWildcards = True
          .Execute Replace:=wdReplaceAll
        End With
      Next
    End With
    Application.ScreenUpdating = True
    End Sub
    Cheers,

    Paul Edstein
    [MS MVP - Word]

Posting Permissions

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