Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 10-11-2024, 07:12 AM
Shelley Lou Shelley Lou is offline VBA Plain Definitions formatted Bold Windows 10 VBA Plain Definitions formatted Bold Office 2016
Expert
VBA Plain Definitions formatted Bold
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA Plain Definitions formatted Bold

I have some code that is supposed to format plain text definitions with quotes, e.g. the "Property" to the "Property" upon selection. I've set the range to Selection.Range but when selecting the text and running the code, it is formatting all plain text within quotes that have not been selected.



The code needs to be able to handle one selection or multiple selections. I'm not sure where I'm going wrong.

Code:
Sub PlainDefinition_FormatBold()
'Select one or more definitions with plain text and plain quotes and format bold
Dim rng As Range
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
Set rng = Selection.Range
With rng.Find
   .ClearFormatting
   .Replacement.ClearFormatting
   .Forward = False
   .Wrap = wdFindStop
   .Format = True
   .MatchWildcards = True
   .text = "[""""]*[""""]" 'Find plain text within plain quotes
   .Replacement.text = "^&" 'Format bold
   .Replacement.Font.Bold = True 'Replacement text is bold
   .Execute Replace:=wdReplaceAll
   .text = "[\(\)]" 'Find plain text with plain quotes within brackets
   .Replacement.text = "^&" 'Format bold
   .Replacement.Font.Bold = False
   .Execute Replace:=wdReplaceAll
   Application.ScreenUpdating = True
   End With
   End Sub
Reply With Quote
  #2  
Old 10-11-2024, 08:32 AM
vivka vivka is offline VBA Plain Definitions formatted Bold Windows 7 64bit VBA Plain Definitions formatted Bold Office 2016
Expert
 
Join Date: Jul 2023
Posts: 294
vivka is on a distinguished road
Default

Hi, Shelley Lou! 1) It would be nice to get your sample doc (this would remove the need to make a test text with all possible variations); 2) do you need to embolden all occurrences of the selected string or sth other? 3) .text = "[\(\)]" will search for round brackets that come together. This .text = "\([“""][A-Za-z ]@[""”]\)" should search for words & spaces within quotes within round brackets.
Reply With Quote
  #3  
Old 10-11-2024, 08:44 AM
Shelley Lou Shelley Lou is offline VBA Plain Definitions formatted Bold Windows 10 VBA Plain Definitions formatted Bold Office 2016
Expert
VBA Plain Definitions formatted Bold
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA Plain Definitions formatted bold

Hi Vivka, I have attached a test document. I want to be able to select just one plain text plain quote or select multiple if they are close together that need to be formatted - it must be on selection though. The plain text plain quotes are displayed on their own or maybe within brackets.

Test Plain Text with Plain Quotes Code.docx

Last edited by Shelley Lou; 10-11-2024 at 08:53 AM. Reason: Adding test document
Reply With Quote
  #4  
Old 10-11-2024, 09:52 AM
vivka vivka is offline VBA Plain Definitions formatted Bold Windows 7 64bit VBA Plain Definitions formatted Bold Office 2016
Expert
 
Join Date: Jul 2023
Posts: 294
vivka is on a distinguished road
Default

Shelley Lou, please, try this:
Code:
Sub PlainDefinition_FormatBold()
'Select one or more definitions with plain text and plain quotes and format bold
Dim rng As range
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
Set rng = selection.range
With rng.Find
   .ClearFormatting
   .Replacement.ClearFormatting
   .Forward = False
   .Wrap = wdFindStop
   .Format = True
   .MatchWildcards = True
   .text = "[""""]*[""""]" 'Find any text within quotes
   .Replacement.Font.Bold = True 'Replacement text is bold
   .Execute Replace:=wdReplaceAll
   .text = "\([""""]*[""""]\)" 'Find any text within quotes within brackets
   .Replacement.Font.Bold = True
   .Execute Replace:=wdReplaceAll
   End With
   Application.ScreenUpdating = True
  Set rng = Nothing
 End Sub
Note: instead of """" you may use "" in all occurrences. Tip: If you want to embolden a selected range, you don't need a macro - just press Alt+B.
And the final remark: in your sample (...by the Landlord to the Tenant in writing") the " is an orphan (= it is not paired), so it could cause false emboldening.
Reply With Quote
  #5  
Old 10-14-2024, 03:51 AM
Shelley Lou Shelley Lou is offline VBA Plain Definitions formatted Bold Windows 10 VBA Plain Definitions formatted Bold Office 2016
Expert
VBA Plain Definitions formatted Bold
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA Plain Definitions formatted bold

Thanks Vivka, much appreciated
Reply With Quote
  #6  
Old 10-14-2024, 05:58 AM
gmaxey gmaxey is offline VBA Plain Definitions formatted Bold Windows 10 VBA Plain Definitions formatted Bold Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,602
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Shellley, Vivka


While not a perfect check, you could add a simple function to help check for paired quotes:

Code:
Sub PlainDefinition_FormatBold()
'Select one or more definitions with plain text and plain quotes and format bold
Dim rng As Range
  Application.ScreenUpdating = False
  If Selection.Type = wdSelectionIP Then
    MsgBox "You have not selected any text.", vbInformation + vbOKOnly, "Invalid selection"
    Exit Sub
  Else
    Set rng = Selection.Range
    If fcnPaired(rng.Text) Then
      With rng.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Forward = False
        .Wrap = wdFindStop
        .Format = True
        .MatchWildcards = True
        .Text = "[""""]*[""""]" 'Find any text within quotes
        .Replacement.Font.Bold = True 'Replacement text is bold
        .Execute Replace:=wdReplaceAll
        .Text = "\([""""]*[""""]\)" 'Find any text within quotes within brackets
        .Replacement.Font.Bold = True
        .Execute Replace:=wdReplaceAll
      End With
      Application.ScreenUpdating = True
    Else
      MsgBox "Quotes in the selected text are not properly paired." & vbCr + vbCr _
           & "Select text containing only properly enclosed quoted text and try again.", vbInformation + vbOKOnly, "OPEN QUOTE"
    End If
    Set rng = Nothing
  End If
lbl_Exit:
  Exit Sub
End Sub

Function fcnPaired(strCheck As String) As Boolean
Dim arrCheck() As String
  fcnPaired = False
  arrCheck = Split(strCheck, Chr(34))
  If UBound(arrCheck) Mod 2 = 0 Then
    fcnPaired = True
  End If
lbl_Exit:
  Exit Function
End Function
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #7  
Old 10-14-2024, 10:17 AM
vivka vivka is offline VBA Plain Definitions formatted Bold Windows 7 64bit VBA Plain Definitions formatted Bold Office 2016
Expert
 
Join Date: Jul 2023
Posts: 294
vivka is on a distinguished road
Default

Thank you, Greg, for a very useful and interesting addition!
Reply With Quote
  #8  
Old 10-17-2024, 11:38 PM
Shelley Lou Shelley Lou is offline VBA Plain Definitions formatted Bold Windows 10 VBA Plain Definitions formatted Bold Office 2016
Expert
VBA Plain Definitions formatted Bold
 
Join Date: Dec 2020
Posts: 259
Shelley Lou is on a distinguished road
Default VBA Plain Definitions formatted bold

Thank you Greg, this is really useful when converting pdfs to Word which can throw these types of errors up
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA Plain Definitions formatted Bold VBA format plain quotes to bold quotes Shelley Lou Word VBA 3 02-11-2024 05:56 AM
Bold Header if it is not Bold (Check to see if it is Bold) armendarizj Word 2 01-06-2022 05:45 PM
VBA Plain Definitions formatted Bold Help with Definitions Macro Shelley Lou Word VBA 3 12-11-2020 01:39 AM
VBA Plain Definitions formatted Bold Replacing formatted bullet points as a plain text character? onlywonderboy Word VBA 6 02-21-2017 03:20 PM
VBA Plain Definitions formatted Bold How to update fields in all header definitions ChrisBrewster Word VBA 2 02-10-2014 10:33 AM

Other Forums: Access Forums

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