Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

changing color and font of specific cells in word table with vba

Tags:

ms-word

vba

I am trying to set up a new table at the end of my document and format it to my specifications. But the backgroundcolor and the textcolor do not seem to work. The Font size also is not exactly what I want, since it applies to the whole table and not only one cell.

This is what I have so far:

Dim myRange As Object
Set myRange = ActiveDocument.Content
myRange.Collapse Direction:=wdCollapseEnd
ActiveDocument.Tables.Add Range:=myRange, NumRows:=3, NumColumns:=2
With .Tables(.Tables.Count)
    .Cell(1, 1).Select
    With Selection
        .Shading.Texture = wdTextureNone
        .Shading.ForegroundPatternColor = wdColorWhite
        .Shading.BackgroundPatternColor = wdColorGray25
        .Font.Size = 14
        .Font.Bold = True
        .Text = "Hello World"
    End With
End With

I want the first row of the table without borders and with font 14, bold, white text on gray background.

like image 826
Kazschuri Avatar asked Aug 14 '15 12:08

Kazschuri


People also ask

How do I change the color of a cell in a table in Word?

Select the cells in which you want to add or change the fill color. On the Tables tab, under Table Styles, click the arrow next to Fill. On the Fill menu, click the color you want.


1 Answers

I found the Answer.

The solution is as follows:

With .Tables(.Tables.Count)        
    With .Cell(1, 1)
        .Shading.BackgroundPatternColor = wdColorGray50
        With .Range
            With .Font 
                .TextColor = wdColorWhite
                .Size = 18
                .Bold = True
            End With
            .Text = "Hello World"
        End With
    End With            
End With

I removed the selection of the cell and used it directly. But the real thing was, the use of .Range when applying .Font and .Text

like image 172
Kazschuri Avatar answered Sep 21 '22 18:09

Kazschuri