PDA

View Full Version : [SOLVED:] VBA to bold all Initial Capped defined terms



disco
04-06-2021, 02:54 AM
I want to bold all text within quotation marks that are ALL CAPS and Initial Caps, but leaves all lowercase terms alone. So it will bold “AGREEMENT” and “Agreement”, but not “agreement”. I made this code, but for some reason, it skips some defined terms that are in Initial Caps. Any ideas?

Sub ConvertsBold()

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Bold = True
With Selection.Find
.Text = "(" & ChrW(8220) & ")([A-Z][a-z]{1,})(" & ChrW(8221) & ")"
.Replacement.Text = ChrW(8220) & "\2" & ChrW(8221)
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Font.Bold = True
Selection.Find.Replacement.ClearFormatting
With Selection.Find.Replacement.Font
.Bold = False
.Italic = False
End With
With Selection.Find
.Text = """"
.Replacement.Text = """"
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

macropod
04-06-2021, 04:34 AM
Your Find expression will not find terms that are all-caps or terms that consist of more than one word. Your code is also inefficient and unnecessarily verbose. Try:

Sub ConvertsBold()
Application.ScreenUpdating = False
With ActiveDocument.Range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Format = True
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
.Replacement.Font.Bold = True
.Text = "[“""][A-Z][A-Za-z ]@[""”]"
.Replacement.Text = "^&"
.Execute Replace:=wdReplaceAll
.Font.Bold = True
.Replacement.Font.Bold = False
.Text = "[“""”]"
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub
PS: When posting code, please structure your code and use the code tags, indicated by the # button on the posting menu. Without the code tags, your code loses much of whatever structure it had.

disco
04-06-2021, 12:38 PM
You are absolutely amazing. Thank you so much. You have no idea how much time you've just saved me. Thank you, thank you, thank you!!