Format based on find code

Capitala

Member
Local time
Today, 00:43
Joined
Oct 21, 2021
Messages
85
Hi,
I'm using the following code to format the text after the column sign (:) as bold. The issued is that it goes through the whole document, while I need it to run on selected range only (table, paragraph(s)...etc).

Thanks for your usual support
 
Dim bWC As Boolean
Dim oRng As Word.range
Set oRng = Selection.range
With oRng.Find
.MatchWildcards = True
.Text = ":"
.Wrap = wdFindStop
While .Execute
oRng.Select
.......... > format code
 
Well I would expect you need to identify that start and end positions, and make that the rng to format?
Not done very much in Word VBA, but that is how I would approach it.

Seems ChatGPT agrees :-)
Code:
Sub BoldTextAfterColon()
    Dim para As Paragraph
    Dim colonPos As Long
    Dim paraRange As Range
    Dim boldRange As Range
    
    ' Loop through each paragraph in the document
    For Each para In ActiveDocument.Paragraphs
        Set paraRange = para.Range
        colonPos = InStr(paraRange.Text, ":")
        
        ' If a colon is found
        If colonPos > 0 Then
            Set boldRange = paraRange.Duplicate
            With boldRange
                .Start = paraRange.Start + colonPos - 1 ' Set start at colon
                .End = paraRange.End - 1 ' Avoid paragraph mark
                .Font.Bold = True
            End With
        End If
    Next para

    MsgBox "Formatting complete.", vbInformation
End Sub

I asked from colon to end of paragraph. You amend as you need.
 
For clarification,
Attached a screenshot of word document containing two blocks (First Block and Second Block)
I need to select the text under first block and apply the code only to selected block. i.e. second block should not be affected.
Regards,
 

Attachments

  • doc1.png
    doc1.png
    10.2 KB · Views: 13
Where is that data coming from?
If Mailmerge, you could just format the field?
 
Not mail merged, it's only typed
Well see what that code does then?
Would it consider Male or 1111 as end of paragraph?
Else you would need to search for a space from colon onwards and use that for the bold length.
 
In fact, if that is a template (and probably should be) and you enter the data after the colon, you can make that bold and then enter new data.

1746097189370.png

1746097213750.png
 
indeed the word or words before the column (:) should be bold, not after the column.
These particulars widely change whether within the same document or other documents.
The first above code which I inserted is working fine but it applies to all the document.
All what I need to limit running the code (range) to selected block of text only
 

Users who are viewing this thread

Back
Top Bottom