View Single Post
 
Old 11-18-2024, 02:26 AM
Shelley Lou Shelley Lou is offline Windows 10 Office 2016
Expert
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA Bold text at beginning of paragraph

I have some code that finds text before the first colon or tab at the beginning of paragraphs and formats bold for definitions. I've found an issue in that if there is an opening square bracket at the beginning of the paragraph, the text remains plain after the code has run. I've tried adding ([\[]) before ([a-zA-Z0-9] but that only converted the text with the square bracket bold and left everything else. How can I get the code to include the square bracket when running the code?

Before code has run
Before Image.JPG

After code has run
After image.JPG

Test doc
Test Doc - Bold text before colon or tab.docx

Code:
Sub Test_BoldText()
Dim oRng As Range
Set oRng = ActiveDocument.Range
  With oRng.Find
    .ClearFormatting
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchWildcards = True
    .Replacement.ClearFormatting
    'Find text before first tab or colon and format bold
    .text = "([a-zA-Z0-9][!^13]@)([^t:])"
    While .Execute
       If oRng.Characters(1).Start = oRng.Paragraphs(1).Range.Characters(1).Start Then
         oRng.Select
         oRng.Font.Bold = True
         If Not oRng.Characters.Last.Next = vbTab Then
           oRng.text = Replace(oRng.text, ":", vbTab)
         Else
           oRng.Characters.Last.Delete
         End If
       End If
    Wend
  End With
  End Sub
Reply With Quote