Results 1 to 2 of 2
  1. #1
    New Lounger
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Macro to replace text with text that has formatting (Word XP)

    I can't for the life of me figure out why this is not working. When I record a macro to replace "~" with "To:" and color it black, it doesn't work. It changes the text but not the color.

    Weird thing is, when I am recording the macro, it works perfectly. But I hit undo, and play the macro, it only changes the text but not the color. Here's the code below and as you can see, there's no color change to be found but the "Format" is set to True. How to fix this?

    Sub Macro2()
    '
    ' Macro2 Macro
    ' Macro recorded 9/5/2011 by Turbocomputers
    '
    Application.Move Left:=366, Top:=21
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = "~"
    .Replacement.Text = "To:"
    .Forward = True
    .Wrap = wdFindContinue
    .Format = True
    .MatchCase = False
    .MatchWholeWord = False
    .MatchKashida = False
    .MatchDiacritics = False
    .MatchAlefHamza = False
    .MatchControl = False
    .MatchByte = False
    .CorrectHangulEndings = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = False
    .MatchFuzzy = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    End Sub

    Thanks in advance.

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts
    Welcome to the Lounge!

    If the text you're searching for is all of a specific color (in the following example, red) then you can try adding the following two lines:
    Code:
    .Font.Color = wdColorRed
    .Replacement.Font.Color = wdColorBlack
    anywhere inside the 'With Selection.Find/End With' structure.

    Gary

Posting Permissions

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