vbams-wordhighlightmaven-shade-plugin

Word VBA Convert Highlight to Shade (1) How to limit Code to Selected Range? (2) Is there Code more efficient than Char by Character+ loop each color?


Title says it all: Can convert Highlight to Shading learnt from Convert Texts from Highlighted to Shaded via VBA

Want to apply to Selection rather than whole document. Also takes a long time to execute character by character for each of 16 base colour Highlights.

Coders probably know a far more efficient routine.

    Sub ConvertHighlightToShade()
      Dim objCharacterRange As Range
      For Each objCharacterRange In ActiveDocument.Characters
            If objCharacterRange.HighlightColorIndex = wdDarkBlue Then
          objCharacterRange.HighlightColorIndex = wdNoHighlight
          objCharacterRange.Shading.BackgroundPatternColor = RGB(0, 0, 128) 'DarkBlue
               Debug.Print "1of16", "Conv Hlgt t Shad.1of16", "#000080", "DarkBlue"
        End If
      Next objCharacterRange
      For Each objCharacterRange In ActiveDocument.Characters
            If objCharacterRange.HighlightColorIndex = wdGreen Then
          objCharacterRange.HighlightColorIndex = wdNoHighlight
          objCharacterRange.Shading.BackgroundPatternColor = RGB(0, 128, 0) 'Green
               Debug.Print "4of16", "Conv Hlgt t Shad.4of16", "#008000", "Green"
        End If
      Next objCharacterRange
      End Sub

I tried use the following model to isolate the Selection area, but variations failed.

https://www.bettersolutions.com/word/characters/vba-range-object.htm

Redefining a Range Object

You can use the SetRange method to redefine an existing Range object The following example defines a range object to the be equal to the current selection and then redefines it to refer to the current selection plus the next 10 characters.

    Dim objRange As Range 
    Set objRange = ActiveWindow.Selection.Range 
    objRange.SetRange(Start:=objRange.Start, _ 
                  End:=objRange.End+10)

I note the advantages of using a Range appear in https://bettersolutions.com/word/characters/vba-range-vs-selection.htm


Solution

  • There is no need to crawl through the document character by character. Word's built-in Find function will find the highlighted text for you.

    The routine below replaces the highlight with shading of the same color.

    Sub ConvertHighlightToShade()
        Dim findRange As Range: Set findRange = Selection.Range
        With findRange
            With .Find
                .ClearFormatting
                .Highlight = True
                .Forward = True
                .Wrap = wdFindStop
            End With
            Do While .Find.Execute
                If findRange.InRange(Selection.Range) Then
                    .Shading.BackgroundPatternColorIndex = .HighlightColorIndex
                    .HighlightColorIndex = wdNoHighlight
                    .Collapse wdCollapseEnd
                Else
                    Exit Do
                End If
            Loop
        End With
    End Sub