To bold all occurrences of a specific word in a Word document, you can use the following VBA code:
- Press ALT + F11 to open the Microsoft Visual Basic for Applications window.
- Click Insert > Module, then copy and paste the following code into the empty module:
Sub BoldAllOccurrences()
Dim targetWord As String
targetWord = InputBox(“Enter the word you want to bold:”, “Bold All Occurrences”)
If Trim(targetWord) = “” Then
MsgBox “Please enter a valid word.”, vbExclamation, “Bold All Occurrences”
Exit Sub
End If
With ActiveDocument.Content.Find
.ClearFormatting
.Text = targetWord
.Replacement.ClearFormatting
.Replacement.Font.Bold = True
.Replacement.Text = “^&”
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute(Replace:=wdReplaceOne)
‘ Continue finding and replacing until no more occurrences are found.
Loop
End With
End Sub
- After inserting the code, press F5 to run the code. A dialog box will appear prompting you to enter the word you want to bold, as shown below:
- Click OK, and all occurrences of the specific word will be bolded throughout the entire document.
Make sure to save your document before running VBA code to avoid unintended data loss.