![]() |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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.
|
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
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 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. |
|
#5
|
|||
|
|||
|
Thanks Vivka, much appreciated
|
|
#6
|
|||
|
|||
|
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
|
|
#7
|
|||
|
|||
|
Thank you, Greg, for a very useful and interesting addition!
|
|
#8
|
|||
|
|||
|
Thank you Greg, this is really useful when converting pdfs to Word which can throw these types of errors up
|
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
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 |
Help with Definitions Macro
|
Shelley Lou | Word VBA | 3 | 12-11-2020 01:39 AM |
Replacing formatted bullet points as a plain text character?
|
onlywonderboy | Word VBA | 6 | 02-21-2017 03:20 PM |
How to update fields in all header definitions
|
ChrisBrewster | Word VBA | 2 | 02-10-2014 10:33 AM |