View Single Post
 
Old 11-23-2022, 04:50 PM
JamesKenwood JamesKenwood is offline Mac OS X Office 2021
Novice
 
Join Date: Nov 2022
Posts: 3
JamesKenwood is on a distinguished road
Default Tagging Highlighted text with appropriate color

I am pretty new to VBA so am trying to find my feet to do a task but can't find a sample anywhere to achieve this.

I need to find all highlighted text in a given colour (lets say yellow for now but will want to do blue too) then tag that text with "[color:yellow]" and [/color]. So I can maintain that information on export to another format.

So far I have the following, but it has problems.
1. I need to run it multiple times to find each highlight, I just want it to process whole document
2. It doesn't find the yellow highlight on lines that have blue and yellow highlights (so highlighting individual words not a whole paragraph)
3. The end tag goes onto the following line

Any guidance would be so much appreciated...

Code:
Sub Highlighting()
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .MatchWildcards = False
        .Forward = True
        .Wrap = wdFindContinue
        .Highlight = True
        Do
        .Execute
          Loop Until Selection.Range.HighlightColorIndex = wdYellow _
          Or Not .Found
            Selection.Range.InsertBefore "[Color: Yellow ]"
            Selection.Range.InsertAfter "[/Color]"
    End With
End Sub
Reply With Quote