简体   繁体   English

如何使用 MS Access 中触发的 VBA 更改 MS Word 文档中形状的颜色?

[英]How do you change the color of a shape in a MS Word document using VBA triggered in MS Access?

I'm using Access to open a word document and populate some fields in Word using data from Access.我正在使用 Access 打开 Word 文档并使用 Access 中的数据填充 Word 中的某些字段。 Here's that code (all working ok so far):这是该代码(到目前为止一切正常):

Private Sub cmdPopulateWord_Click()

    Dim appWord As Word.Application
    Dim doc As Word.Document
    Dim TestProspCode As String

    On Error Resume Next
    Err.Clear

    Set appWord = GetObject(, "Word.Application")

    If Err.Number <> 0 Then

        Set appWord = New Word.Application

    End If

    Set doc = appWord.Documents.Open("H:\Populate Word Document from Access.docx", , True)

    With doc

        .FormFields("wtxID").Result = Me!ID
        .FormFields("wtxFirstName").Result = Me!FirstName
        .FormFields("wtxLastName").Result = Me!LastName
        .FormFields("wtxDoB").Result = Me!DateOfBirth
        .FormFields("wtxProspCode").Result = Forms!tblWordDoc!tblProspCode_sub!ProspectusCode 
        .FormFields("wtxCourse").Result = Forms!tblWordDoc!tblProspCode_sub!Course

        .Visible = True
        .Activate

    End With

    Set doc = Nothing

    Set appWord = Nothing

    Exit Sub

errHandler:
MsgBox Err.Number & ": " & Err.Description

End Sub

I'm trying to see how I can also change the colour of shape already in the same Word document referenced in the above code.我正在尝试查看如何更改上述代码中引用的同一个 Word 文档中已有的形状颜色。

Referring to some info here , I've tried inserting the code below straight after the with in the code above.参考这里的一些信息,我尝试在上面代码中的with之后直接插入下面的代码。

With doc

    .Shapes("Rounded Rectange 1").Fill.BackColor.RGB = RGB(0, 0, 0)
    .Visible = msoTrue

End With

There's no error, but the shape's colour does not change to black.没有错误,但形状的颜色不会变为黑色。

What you are possibly looking for is .ForeColor property instead of .BackColor .您可能正在寻找的是.ForeColor property而不是.BackColor See the code below where I additionally show how to change a border of the shape to make it look nice.请参阅下面的代码,其中我还展示了如何更改形状的边框以使其看起来更美观。

With doc.Shapes("Rounded Rectangle 1")
    'dark grey, (0,0,0) for black
    .Fill.ForeColor.RGB = RGB(80, 80, 80)
    'black borders
    .Line.ForeColor.RGB = RGB(0, 0, 0)
End With

根据 Remou 使用 MS_Word 宏记录器的提示,我发现需要引用矩形形状及其背景颜色如下:

.Shapes.Range(Array("Rounded Rectangle 1")).Fill.ForeColor.RGB = RGB(0, 0, 0)

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

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