Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #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
  #2  
Old 11-18-2024, 02:58 AM
macropod's Avatar
macropod macropod is online now VBA Bold text at beginning of paragraph Windows 10 VBA Bold text at beginning of paragraph Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,343
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
Sub BoldText()
Application.ScreenUpdating = False
With ActiveDocument
  With .Range
    .InsertBefore vbCr
    With .Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Forward = True
      .Format = False
      .Wrap = wdFindStop
      .MatchWildcards = True
      .Text = "^13[!^13]@[^t:]"
    End With
    Do While .Find.Execute
      .Start = .Start + 1
      .End = .End - 1
      .Font.Bold = True
      .Characters.Last.Next = vbTab
      .Collapse wdCollapseEnd
    Loop
  End With
  .Range.Characters.First.Delete
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 11-18-2024, 06:54 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

Oh wow, amazing, thank you Macropod, works a treat
Reply With Quote
  #4  
Old 11-18-2024, 10:44 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 think I spoke too soon as I've tested the code again on another document I'm working on and its now making all the manual sub level numbering bold. I will go have to go back to my original code and figure out how to update the IF Statement to include to move one space if paragraph starts with an opening square bracket. Completely my bad as I didn't originally include any sub levels within the test document or images so my apologies for that.

After.JPG
Reply With Quote
  #5  
Old 11-18-2024, 01:23 PM
macropod's Avatar
macropod macropod is online now VBA Bold text at beginning of paragraph Windows 10 VBA Bold text at beginning of paragraph Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,343
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

That would only happen if your sub-level #s were typed. Why aren't you using auto-numbering for that? In any event, it's easily solved by using:
.Text = "^13[!^13\(]@[^t:]"
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 11-18-2024, 02:32 PM
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

Hi Macropod, thanks for replying and updating the line of code which I've now inserted into my much larger code I use to format the definitions. I then have another code which converts the definitions into a 2 column table and auto numbers the sub levels but for the first part the numbering needs to be manual in order to insert the tab before them ready for the table code.
Reply With Quote
  #7  
Old 11-18-2024, 03:15 PM
macropod's Avatar
macropod macropod is online now VBA Bold text at beginning of paragraph Windows 10 VBA Bold text at beginning of paragraph Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,343
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Quote:
Originally Posted by Shelley Lou View Post
I then have another code which converts the definitions into a 2 column table and auto numbers the sub levels but for the first part the numbering needs to be manual in order to insert the tab before them ready for the table code.
My approach would be to use the auto-numbering so that you can add/delete clauses as needed, then use:
.Range.ListFormat.ConvertNumbersToText
on the range that's to be converted to a table.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply



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 03:28 PM.


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