Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 07-12-2024, 03:30 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

I want to change the code below to automatically add bold quotes to the bold text within brackets. At the moment I have to select multiple text to add the quotes. Advice always appreciated.



Before
Before.JPG

After
After.JPG

Code:
Sub BoldTextWithinBrackets_AddQuotes()
'Select multiple bold definitions and apply bold quotes
Application.ScreenUpdating = False
If Selection.Type = wdSelectionIP Then
 'MsgBox Prompt:="You have not selected any text!"
 MsgBox "You have not selected any text!", vbExclamation, "Invalid selection"
 Exit Sub
End If
With Selection.Find
   .ClearFormatting
   .Replacement.ClearFormatting
   .Forward = False
   .Wrap = wdFindStop
   .Format = True
   .MatchWildcards = False
    With Selection.Find
        .text = ""
        .Font.Bold = True
        .Replacement.text = """^&"""
        .Execute Replace:=wdReplaceAll
    End With
    Application.ScreenUpdating = True
   End With
End Sub
Reply With Quote
  #2  
Old 07-12-2024, 05:44 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,598
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Sub BoldTextWithinBrackets_AddQuotes()
Dim oRng As Range
Set oRng = ActiveDocument.Range
Application.ScreenUpdating = False
With oRng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = False
.Wrap = wdFindStop
.Format = True
.MatchWildcards = False
.Font.Bold = True
.Replacement.Text = """^&"""
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
lbl_Exit:
Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 07-12-2024, 06:23 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

Hi Greg, thank you so much for replying and supplying the code - unfortunately the code adds quotes to all bold text and I only want to add quotes if the bold text is within parenthesis (brackets) - not sure how to look for this in code.
Reply With Quote
  #4  
Old 07-12-2024, 07:43 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,598
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Code:
Sub 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
      oRng.Select
      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
      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/
Reply With Quote
  #5  
Old 07-12-2024, 07:59 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

Hi Greg, have just run the code but a debug message came up on the If oRng.Characters.First.Previous = Chr(40) And oRng.Characters.Last.Next = Chr(41)Then line - not sure why.


Capture.JPG
Reply With Quote
  #6  
Old 07-12-2024, 08:26 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,598
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

That would happen it the first word in the document was bold and no open paren preceding it.


Code:
Sub 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
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 07-12-2024, 08:43 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

Hi Greg, yes the first words were bold in the document, thank you so much for the updated code, works perfectly.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
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
Microsoft Word macro to find text, select all text between brackets, and delete helal1990 Word VBA 4 02-05-2015 03:52 PM

Other Forums: Access Forums

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