user6096423
user6096423

Reputation: 141

MS WORD 2010 Using VBA to type text into table cell then reformat and type more text w/o deleting previous text

I am using this code which specifically places the text in the precise cell I want using this code:

    Dim myText1 As String
    Dim myText2 As String
    myText1 = "Header"
    myText2 = "Body"

    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 & myText2
End With

The problem I am having is "myText2" is not supposed to be underlined or bold.

I have tried this:

    Dim myText1 As String
    Dim myText2 As String
    myText1 = "Header"
    myText2 = "Body"

    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  
    .Text = myText2

End With

But what happens is the first myText1 gets deleted and all I am left with is myText2.

and this

    With ActiveDocument.Tables(1).Cell(2, 2).Range
       .Font.Name = "Times New Roman"
.Font.Size = 12
.Font.Bold = True
.Font.Underline = True
.InsertAfter myText1 & vbCr & vbCr
.Font.Bold = False
.Font.Underline = False    
.InsertAfter myText2

While this appends the text, the formatting for the entire post is no underline or bold, when the end result is supposed to look like

Header

Body

How can I reformat myText2, have it post, without losing the formatted myText1 above?

Upvotes: 1

Views: 12928

Answers (2)

Timothy Rylatt
Timothy Rylatt

Reputation: 7850

In your code you have set the With statement to work with the entire range of the cell. This results in the formatting being applied to the entire cell.

You don't have to use the Selection object to apply formatting, you just need to make sure that you are working with the correct range. Using the Selection object makes the code run more slowly as it moves the cursor around.

I have rewritten your code below.

Sub AddTextToCell()
    Dim myText1 As String
    Dim myText2 As String
    myText1 = "Header"
    myText2 = "Body"

    With ActiveDocument.Tables(1).Cell(2, 2).Range
        .Text = myText1 & vbCr & vbCr & myText2
        With .Font
            .Name = "Times New Roman"
            .Size = 12
            .Bold = False
            .Underline = False
        End With
        With .Paragraphs.First.Range.Font
            .Bold = True
            .Underline = True
        End With
    End With
End Sub

Upvotes: 4

Olle Sjögren
Olle Sjögren

Reputation: 5385

Normally it is better to enter text the way you do, without using Select, but when applying different formats to different parts of a cell I think you have to use it. I had to change the order of the formatting and step around a bit in the document to make it work:

    Dim myText1 As String
    Dim myText2 As String
    myText1 = "Header"
    myText2 = "Body"

    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
    End With

    'Select the whole cell
    ActiveDocument.Tables(1).Cell(2, 2).Select

    'Move to the right
    Selection.Collapse Direction:=wdCollapseEnd
    'Move back to the left
    Selection.MoveLeft wdCharacter, 1

    'Add the text (using the myText1 format)
    Selection.Range.Text = myText2

    'Select the on word the right (myText2)
    Selection.MoveRight wdWord, 1, True

    'Format myText2
    Selection.Range.Font.Underline = False
    Selection.Range.Font.Bold = False

Upvotes: 1

Related Questions