![]() |
|
#1
|
|||
|
|||
|
I have some code that finds text before the first colon or tab at the beginning of paragraphs and formats bold for definitions. I've found an issue in that if there is an opening square bracket at the beginning of the paragraph, the text remains plain after the code has run. I've tried adding ([\[]) before ([a-zA-Z0-9] but that only converted the text with the square bracket bold and left everything else. How can I get the code to include the square bracket when running the code?
Before code has run Before Image.JPG After code has run After image.JPG Test doc Test Doc - Bold text before colon or tab.docx Code:
Sub Test_BoldText()
Dim oRng As Range
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
End Sub
|
|
#2
|
||||
|
||||
|
Try:
Code:
Sub BoldText()
Application.ScreenUpdating = False
With ActiveDocument
With .Range
.InsertBefore vbCr
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Format = False
.Wrap = wdFindStop
.MatchWildcards = True
.Text = "^13[!^13]@[^t:]"
End With
Do While .Find.Execute
.Start = .Start + 1
.End = .End - 1
.Font.Bold = True
.Characters.Last.Next = vbTab
.Collapse wdCollapseEnd
Loop
End With
.Range.Characters.First.Delete
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#3
|
|||
|
|||
|
Oh wow, amazing, thank you Macropod, works a treat
|
|
#4
|
|||
|
|||
|
I think I spoke too soon as I've tested the code again on another document I'm working on and its now making all the manual sub level numbering bold. I will go have to go back to my original code and figure out how to update the IF Statement to include to move one space if paragraph starts with an opening square bracket. Completely my bad as I didn't originally include any sub levels within the test document or images so my apologies for that.
After.JPG |
|
#5
|
||||
|
||||
|
That would only happen if your sub-level #s were typed. Why aren't you using auto-numbering for that? In any event, it's easily solved by using:
.Text = "^13[!^13\(]@[^t:]"
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
#6
|
|||
|
|||
|
Hi Macropod, thanks for replying and updating the line of code which I've now inserted into my much larger code I use to format the definitions. I then have another code which converts the definitions into a 2 column table and auto numbers the sub levels but for the first part the numbering needs to be manual in order to insert the tab before them ready for the table code.
|
|
#7
|
||||
|
||||
|
Quote:
.Range.ListFormat.ConvertNumbersToText on the range that's to be converted to a table.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Automatically put a text string at the beginning of a paragraph | jthomas666 | Word | 2 | 08-22-2019 12:27 PM |
Word macro to insert text at the beginning of paragraph but skip tables
|
ashalles135 | Word VBA | 5 | 09-26-2018 09:49 AM |
word macro To insert text at the beginning and at end of paragraph
|
ArieH | Word VBA | 20 | 09-10-2017 04:23 PM |
Macro to Insert text into the beginning on specific paragraphs unless the paragraph is blank
|
caboy | Word VBA | 2 | 04-01-2015 07:00 AM |
| Looping macros to add text to beginning and end of a paragraph | pachmarhi | Word VBA | 0 | 02-16-2009 06:57 AM |