Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-25-2024, 08:13 AM
Shelley Lou Shelley Lou is offline VBA Find Bold Text within Brackets and Add Quotes Windows 10 VBA Find Bold Text within Brackets and Add Quotes Office 2016
Expert
VBA Find Bold Text within Brackets and Add Quotes
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA Find Bold Text within Brackets and Add Quotes

A while ago, Greg very kindly updated some code for me to add quotes to bold text within brackets (see link below). At the time, the documents I was working on had an opening bracket bold text closing bracket. I'm now working on documents where there is also plain text with bold text within the brackets e.g. (the Tenant) or (the Property).



What can I add to the code to ignore the plain text and just add double bold quotes to the bold text?

https://www.msofficeforums.com/word-...ckets-add.html

Code:
Sub DPU_BoldTextWithinBrackets_AddQuotes()
Dim oRng As Range
  Set oRng = ActiveDocument.Range
  Application.ScreenUpdating = False
  With oRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Wrap = wdFindStop
    .Format = True
    .MatchWildcards = False
    .Font.Bold = True
    While .Execute
      On Error GoTo Err_Handler
      If oRng.Characters.First.Previous = Chr(40) And oRng.Characters.Last.Next = Chr(41) Then
        oRng.InsertBefore Chr(34)
        oRng.Characters.First.Font.Bold = True
        oRng.InsertAfter Chr(34)
      End If
Err_Handler:
      oRng.Collapse wdCollapseEnd
    Wend
  End With
  Application.ScreenUpdating = True
lbl_Exit:
  Exit Sub
End Sub
Reply With Quote
  #2  
Old 09-25-2024, 10:40 AM
gmaxey gmaxey is offline VBA Find Bold Text within Brackets and Add Quotes Windows 10 VBA Find Bold Text within Brackets and Add Quotes Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,601
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

I think in this case you will need to find within a find:


Code:
Sub DPU_BoldTextWithinBrackets_AddQuotes2()
Dim oRng As Range
Dim oRng2 As Range
  Set oRng = ActiveDocument.Range
  Application.ScreenUpdating = False
  With oRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = "\(*\)"
    .Wrap = wdFindStop
    .Format = True
    .MatchWildcards = True
    While .Execute
      Set oRng2 = oRng.Duplicate
      With oRng2.Find
        .Format = True
        .Font.Bold = True
        Do While .Execute
          If oRng2.InRange(oRng) Then
          If Not oRng2.Characters.First = Chr(34) Then
            oRng2.InsertBefore Chr(34)
            oRng2.Characters.First.Font.Bold = True
            oRng2.InsertAfter Chr(34)
            oRng2.Collapse wdCollapseEnd
          End If
        Else
          Exit Do
        End If
      Loop
      End With
      oRng.Collapse wdCollapseEnd
    Wend
  End With
  Application.ScreenUpdating = True
lbl_Exit:
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/

Last edited by gmaxey; 09-25-2024 at 12:50 PM.
Reply With Quote
  #3  
Old 09-26-2024, 01:15 AM
Shelley Lou Shelley Lou is offline VBA Find Bold Text within Brackets and Add Quotes Windows 10 VBA Find Bold Text within Brackets and Add Quotes Office 2016
Expert
VBA Find Bold Text within Brackets and Add Quotes
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA Find Bold Text within Brackets and Add Quotes

Thank you so much Greg, works perfectly.

I've got an unrelated question regarding Word. I used to be able to select one cross reference/hyperlink field and use Control Shift F9 to unlink the hyperlink. Has Microsoft changed this as when I do Control Shift F9 now it unlinks every single cross reference/hyperlink in the document even the table of contents, it is really baffling if they have changed this?
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Find Bold Text within Brackets and Add Quotes VBA Find Bold Text within Brackets and Add Quotes Shelley Lou Word VBA 6 07-12-2024 08:43 AM
VBA Find Bold Text within Brackets and Add Quotes VBA format plain quotes to bold quotes Shelley Lou Word VBA 3 02-11-2024 05:56 AM
Subscript and bold number between square brackets and remove square brackets nonno Word 0 02-10-2024 08:23 AM
Find and delet all text within brackets and the brackets themselves ranjan Word VBA 2 07-22-2021 01:06 AM
VBA Find Bold Text within Brackets and Add Quotes find and delet all text within brackets and the brackets themselves wrdy Word 2 08-03-2017 06:55 PM

Other Forums: Access Forums

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