none
MS Word VBA or C# - Replace highlighted text - ONLY inside a range - with its HighlightColorIndex RRS feed

  • Question

  • My following macro replaces all highlighted texts with corresponding HighlightColorIndex of its background color. But I want to do it only for the highlighted texts within substrings enclosed with ~......~.  For example:

    Input:

    Output: [Please refer for color index values at: WdColorIndex Enumeration]

    Desired Output:

    Sub replaceHighlightWithColorIndex()
        
        Selection.HomeKey unit:=wdStory
        With Selection.Find
            .ClearFormatting
            .Highlight = True
            While .Execute(Forward:=True, Format:=True)
                Selection.Text = Selection.Range.HighlightColorIndex
            Wend
        End With
        
    End Sub

    Note: I tried to mimic a similar solution from Paul Edstein for replacing bold within range by changing the .Font.Bold = True to .Highlight = True  and ReplacementText to .Replacement.Text = ActiveDocument.Range.Duplicate.FormattedText.HighlightColorIndex but it replaces the highlighted texts inside ~...~ with 9999999 as shown below:

    Thanks..Nam




    • Edited by namwam Monday, February 1, 2016 3:02 AM
    Monday, February 1, 2016 2:54 AM

Answers

  • Try:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim Rng As Range
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "~*~"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = True
        .Execute
      End With
      Do While .Find.Found
        Set Rng = .Duplicate
        With .Duplicate
          With .Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Highlight = True
            .Forward = True
            .Wrap = wdFindStop
            .Format = True
            .Execute
            End With
            Do While .Find.Found
              If Not .InRange(Rng) Then Exit Do
              .Text = .HighlightColorIndex
              .Collapse wdCollapseEnd
              .Find.Execute
            Loop
        End With
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Set Rng = Nothing
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by namwam Wednesday, February 3, 2016 4:50 AM
    Monday, February 1, 2016 6:38 AM

All replies

  • I'd ask some questions.

    1. Why have you started a new thread?  Why not continue your old thread?
    2. What is a trigger to execute "replaceHighlightWithColorIndex"?
    3. Is it guaranteed that the character "~" group into pairs?  i.e. the number of "~" is even, instead of odd.

    Regards.
    • Edited by Ashidacchi Monday, February 1, 2016 5:51 AM
    Monday, February 1, 2016 5:48 AM
  • Try:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim Rng As Range
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "~*~"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchWildcards = True
        .Execute
      End With
      Do While .Find.Found
        Set Rng = .Duplicate
        With .Duplicate
          With .Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Highlight = True
            .Forward = True
            .Wrap = wdFindStop
            .Format = True
            .Execute
            End With
            Do While .Find.Found
              If Not .InRange(Rng) Then Exit Do
              .Text = .HighlightColorIndex
              .Collapse wdCollapseEnd
              .Find.Execute
            Loop
        End With
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    Set Rng = Nothing
    Application.ScreenUpdating = True
    End Sub


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by namwam Wednesday, February 3, 2016 4:50 AM
    Monday, February 1, 2016 6:38 AM
  • Paul, it works - thank you. I think the key was the use of:  If Not .InRange(Rng) Then Exit Do. I thought use of  .Wrap = wdFindStop would do it as was the case in your response to Replacing bold only within range But that did not work in the highlight case. -Nam



    • Marked as answer by namwam Wednesday, February 3, 2016 4:50 AM
    • Unmarked as answer by namwam Wednesday, February 3, 2016 4:50 AM
    • Edited by namwam Wednesday, February 3, 2016 5:10 AM
    Wednesday, February 3, 2016 4:49 AM
  • Ashidacchi, thank you for trying to assist. Responses to your inquiry are:

    1. This case turned out to be different than the case of replacing bold. That case worked for italic for example but not for highlight
    2. Once my code finds a match ~.....~ it would pass this range to replaceHighlightWithColorIndex that, in turn, would return the modified range as a replacementText to the original code. I must admit I should have provided a calling code, as well, for more clarity.
    3. Yes. Otherwise, code would either display unexpected result or would generate an error (similar to the case of a missing end tag in an HTML or XML file)

    Thank..Nam

    Wednesday, February 3, 2016 5:05 AM