vbams-word

Formatting Text at Bookmark after Userform Selection


I have a Word document that has a bookmark. I created a userform that inserts text into the document depending on the selection (checkbox 1 or 2).

Can the text being inserted at the bookmark be formatted?

For instance, I'd like the text to be underlined, regardless of selecting checkbox 1 or 2. However, I'd like the text inserted from checkbox 2 to also be bolded.

The name of the bookmark is CalSub.

Dim strSub As String
If CheckBox1 = True Then strSub = "Notice of Data Security Incident "
If CheckBox2 = True Then strSub = "Notice of Data Breach"

'Clean up formatting

With ActiveDocument
    .Bookmarks("CalSub").Range.Text = strSub
End With

Solution

  • Something like this:

    Dim rng As Range, doc As Document
    
    Set doc = ActiveDocument
    Set rng = doc.Bookmarks("CalSub").Range  'get the range
    rng.Text = strSub                        'set the text (note this will delete the bookmark)
    doc.Bookmarks.Add "CalSub", rng          'recreate bookmark          
    rng.Font.Underline = True                'always underline
    If CheckBox2 Then rng.Font.Bold = True   'set bold depending on value of checkbox