简体   繁体   English

使用vba更改字表中特定单元格的颜色和字体

[英]changing color and font of specific cells in word table with 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. 但backgroundcolor和textcolor似乎不起作用。 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. 我希望表格的第一行没有边框,而灰色背景上的字体为14,粗体,白色文字。

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 但真正的事情是,使用.Range申请时.Font.Text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM