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

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
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,226,730
Messages
6,192,705
Members
453,748
Latest member
akhtarf3

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