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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32010286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:54:12  来源:igfitidea点击:

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

vbams-wordword-vba

提问by Kazschuri

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.

我正在尝试在我的文档末尾设置一个新表格并将其格式化为我的规范。但是 backgroundcolor 和 textcolor 似乎不起作用。字体大小也不是我想要的,因为它适用于整个表格,而不仅仅是一个单元格。

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,灰色背景上的粗体白色文本。

采纳答案by Kazschuri

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 .Rangewhen applying .Fontand .Text

我删除了单元格的选择并直接使用它。但真正的事情是,.Range在申请.Font.Text