Consulting

Results 1 to 3 of 3

Thread: VBA to bold all Initial Capped defined terms

  1. #1
    VBAX Newbie
    Joined
    Mar 2021
    Posts
    6
    Location

    VBA to bold all Initial Capped defined terms

    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
    Last edited by macropod; 04-06-2021 at 04:23 AM. Reason: Added code tags & formatting

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    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.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Newbie
    Joined
    Mar 2021
    Posts
    6
    Location
    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!!
    Last edited by macropod; 04-06-2021 at 01:52 PM. Reason: New question split to new thread

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •