Search code examples
vbams-wordasianfonts

How can I make this Asian font highlighting macro recognize between-word characters?


For most of the documents I work with, I need to highlight instances of Asian font (such as SimSun, MS Mincho).

I put together the the code below - it works well but it doesn't highlight Asian font characters within non-Asian words (such as the apostrophe in "it's").

Sub HighlightAsian2019()
Dim aWord
    For Each aWord In ActiveDocument.Words
        If aWord.Font.Name = "SimSun" Then
           aWord.FormattedText.HighlightColorIndex = wdTurquoise
        End If
    Next aWord
    For Each aWord In ActiveDocument.Words
        If aWord.Font.Name = "MS Mincho" Then
           aWord.FormattedText.HighlightColorIndex = wdTurquoise
        End If
    Next aWord
End Sub

Can anyone help me improve the code so that ALL instances of SimSun and MS Mincho in the text are highlighted?

Any help will be greatly appreciated!


Solution

  • Use this Loop:

    Sub HighlightAsian2019()
    
    With ActiveDocument.Range
    
        For i = 1 To .Characters.Count
            If .Characters(i).Font.Name = "SimSun" Then
                .Characters(i).FormattedText.HighlightColorIndex = wdTurquoise
            End If
        Next
    
    End With
    
    End Sub
    

    You can use the .Characters property of Range to access the alphabets. Add another If condition for other fonts to check