Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 11-18-2024, 02:26 AM
Shelley Lou Shelley Lou is offline VBA Bold text at beginning of paragraph Windows 10 VBA Bold text at beginning of paragraph Office 2016
Expert
VBA Bold text at beginning of paragraph
 
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
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Automatically put a text string at the beginning of a paragraph jthomas666 Word 2 08-22-2019 12:27 PM
VBA Bold text at beginning of paragraph Word macro to insert text at the beginning of paragraph but skip tables ashalles135 Word VBA 5 09-26-2018 09:49 AM
VBA Bold text at beginning of paragraph word macro To insert text at the beginning and at end of paragraph ArieH Word VBA 20 09-10-2017 04:23 PM
VBA Bold text at beginning of paragraph Macro to Insert text into the beginning on specific paragraphs unless the paragraph is blank caboy Word VBA 2 04-01-2015 07:00 AM
Looping macros to add text to beginning and end of a paragraph pachmarhi Word VBA 0 02-16-2009 06:57 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:18 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft