VBA in Word - Text Format Overwriting with InsertAfter when adding text within a Table Cell

inosent

Board Regular
Joined
Mar 19, 2007
Messages
131
I am using this code to insert an underlined and bold header with a non-formatted paragraph underneath. I am able to append the second paragraph below the header in the cell, but I lose the formatting for the header:

Code:
Sub T1()
Dim myText1 As String
Dim myText2 As String
myText1 = "Header"
myText2 = "A medium sized paragraph with 3-5 sentences. A medium sized paragraph with 3-5 sentences. A medium sized paragraph with 3-5 sentences. "
    
With ActiveDocument.Tables(1).Cell(2, 2).Range
.Font.Name = "Times New Roman"
.Font.Size = 12
.Font.Bold = True
.Font.Underline = True
.Text = myText1 & vbCr & vbCr
.Font.Bold = False
.Font.Underline = False
.InsertAfter myText2

    End With

End Sub

The result is supposed to look like this:

<b><u>Header</u></b>

A medium sized paragraph with 3-5 sentences. A medium sized paragraph with 3-5 sentences. A medium sized paragraph with 3-5 sentences.

But I get:

Header

A medium sized paragraph with 3-5 sentences. A medium sized paragraph with 3-5 sentences. A medium sized paragraph with 3-5 sentences.
 
Last edited:

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Try:
Code:
Sub T1()
Dim myText1 As String, myText2 As String
myText1 = "Header"
myText2 = "A medium sized paragraph with 3-5 sentences. A medium sized paragraph with 3-5 sentences. A medium sized paragraph with 3-5 sentences. "
    
With ActiveDocument.Tables(1).Cell(2, 2).Range
  With .Font
    .Name = "Times New Roman"
    .Size = 12
  End With
  .Text = myText1 & vbCr & vbCr & myText2
  With .Paragraphs.First.Range.Font
    .Bold = True
    .Underline = True
  End With
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,940
Messages
6,122,352
Members
449,080
Latest member
Armadillos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top