top of page

MS Word visual basic code to highlight text not in selected font


The great Allen Wyatt has posted vba code here,

https://word.tips.net/T011069_Finding_Text_Not_Using_a_Particular_Font.html

. . . which you can use to select any text in Word document that is not in a font that you want to retain. This was helpful to me today when I wanted to select just the footnotes in a Word document that had been converted from a PDF file. The converted version did not format the footnotes properly, so other techniques to copy out just the footnotes did not work. The footnotes happened to be in a different font than the text in the body of the document.

Press ALT + F11 to enter Visual Basic, and enter the below vba code in a new module. When the macro is run it will prompt you to enter the name of the font you do not want highlighted.

The text in any other font is highlighted in yellow.

Sub HighlightOtherFonts()

Dim iCounter As Integer

Dim sFontName As String

Dim sPrompt As String

Dim sTitle As String

Dim sDefault As String

Dim c As Range

' Gets the name of the font as typed by the user

sPrompt = "Type the name of the font that is OK to "

sPrompt = sPrompt & "have in the document."

sTitle = "Acceptable Font Name"

sDefault = ActiveDocument.Styles(wdStyleNormal).Font.Name

sFontName = InputBox(sPrompt, sTitle, sDefault)

' Verifies that the name of the font is valid

For Each sFont In Application.FontNames

If UCase(sFontName) = UCase(sFont) Then

' Changes the user-typed name of the font to

' the version recognized by the application

' Example: 'times new roman' (user-typed) is

' changed to 'Times New Roman' (application version)

sFontName = sFont

Exit For

Else

' Terminates the loop if the name of the font is invalid

iCounter = iCounter + 1

If iCounter = FontNames.Count Then

sPrompt = "The font name as typed does not match "

sPrompt = sPrompt & "any fonts available to the "

sPrompt = sPrompt & "application."

sTitle = "Font Name Not Found"

MsgBox sPrompt, vbOKOnly, sTitle

Exit Sub

End If

End If

Next sFont

' Checks each character in the document, highlighting

' if the character's font doesn't match the OK font

For Each c In ActiveDocument.Characters

If c.Font.Name <> sFontName Then

' Highlight the selected range of text in yellow

c.FormattedText.HighlightColorIndex = wdYellow

End If

Next c

End Sub


bottom of page