Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-26-2024, 03:05 AM
Shelley Lou Shelley Lou is offline VBA Format text before colon bold not including square bracket/bracket Windows 10 VBA Format text before colon bold not including square bracket/bracket Office 2016
Expert
VBA Format text before colon bold not including square bracket/bracket
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA Format text before colon bold not including square bracket/bracket

I have some code that formats text before a colon bold for definitions. I'm currently working on a document where there are square brackets and/or opening brackets at the beginning of the paragraph before the colon but the code isn't making the text bold. I did try adding ([\(\[\]) but this didn't work. Any ideas?




Definitions.JPG

Code:
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
Reply With Quote
  #2  
Old 09-26-2024, 05:47 AM
vivka vivka is offline VBA Format text before colon bold not including square bracket/bracket Windows 7 64bit VBA Format text before colon bold not including square bracket/bracket Office 2016
Expert
 
Join Date: Jul 2023
Posts: 293
vivka is on a distinguished road
Default

Hi, Shelley Lou! If need be, change selection to ActiveDocument.

Code:
Sub Embolden_Rng_1()
 'Embolden strings including colons.

 Dim oRng As range
Set oRng = selection.range
  With oRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchWildcards = True
 'Find text from a para start until the 1st tab or colon (including):
    .text = "^13[!^13]@[^t:]"
    .Replacement.Font.Bold = True
    .Execute Replace:=wdReplaceAll
  End With
Set oRng = Nothing
End Sub
Code:
Sub Embolden_Rng_2()
'Embolden strings excluding colons.

Dim oRng As range
Set oRng = selection.range
  With oRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchWildcards = True
'Find text from a para start until the 1st tab or colon:
    .text = "(^13[!^13]@)([^t:])"
    While .Execute And oRng.InRange(selection.range)
        oRng.End = oRng.End - 1
        oRng.Font.Bold = True
        oRng.Collapse wdCollapseEnd
    Wend
  End With
Set oRng = Nothing
 End Sub
Code:
Sub Embolden_Rng_3()
'Embolden strings from para starts until 1st colon/tab
'(excluding colons & possible starting round/square brackets).

Dim oRng As range
Set oRng = selection.range
  With oRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Forward = True
    .Wrap = wdFindStop
    .Format = True
    .MatchWildcards = True
'Find text from a para start until 1st tab/colon:
    .text = "(^13[!^13]@)([^t:])"
    While .Execute And oRng.InRange(selection.range)
        If oRng.Characters(2) Like "[\(\[]" Then oRng.start = oRng.start + 2
        oRng.End = oRng.End - 1
        oRng.Font.Bold = True
        oRng.Collapse wdCollapseEnd
    Wend
  End With
Set oRng = Nothing
End Sub
Reply With Quote
  #3  
Old 09-26-2024, 07:33 AM
Shelley Lou Shelley Lou is offline VBA Format text before colon bold not including square bracket/bracket Windows 10 VBA Format text before colon bold not including square bracket/bracket Office 2016
Expert
VBA Format text before colon bold not including square bracket/bracket
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA Format text before colon bold not including square bracket/bracket

Hi Vivka, thanks so much for your code - unfortunately not what I needed it to do on this occasion but I did take part of your code "([!^13]@)([^t:])" in place of what was in my original code and that has worked how I need it to so really appreciate that.

Code:
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 = "([!^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
Reply With Quote
  #4  
Old 09-26-2024, 09:12 AM
vivka vivka is offline VBA Format text before colon bold not including square bracket/bracket Windows 7 64bit VBA Format text before colon bold not including square bracket/bracket Office 2016
Expert
 
Join Date: Jul 2023
Posts: 293
vivka is on a distinguished road
Default

I'm glad you managed to solve your problem! My final remark: 'oRng.Select' seems redundant.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Format text before colon bold not including square bracket/bracket VBA quote mark to insert after square bracket Shelley Lou Word VBA 13 06-25-2021 12:41 AM
VBA Format text before colon bold not including square bracket/bracket Delete blank paragraph below bold, end square bracket Dave T Word VBA 2 04-28-2019 11:00 PM
Conditional format for NCAA bracket JoemanNville Excel 0 03-25-2019 01:45 PM
VBA Format text before colon bold not including square bracket/bracket Remove repeated number after square bracket jeffreybrown Word VBA 8 12-04-2018 06:01 PM
Word 2010 - Remove square-bracket encased string in large document IntestinalWorm Word 1 06-20-2017 01:14 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:34 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