如何以编程方式更改Word文档的背景颜色?

问题描述:

大家好,
我正在尝试更改Word文档中页面的背景颜色.但是我出错了.这是我的代码.

Hi all,
I am trying to change the background color of a page in word document. But i am getting error. This is my code.

Try
            oWord = GetObject(, "Word.Application")
            oDoc = oWord.ActiveDocument
            Dim ttl As String = oDoc.Name
            Label1.Text = ttl
            Dim clr As Color = Color.FromArgb(128, 128, 0)
            Try
                oDoc.Background.Fill.BackColor.RGB = ColorTranslator.ToOle(clr)
                oDoc.Background.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoCTrue
                oDoc.Background.Fill.Solid()

            Catch ex As Exception
                MsgBox("Second TryCatch - " & ex.Message)
            End Try
        Catch ex As Exception
            Label1.Text = "Object Error"
        End Try



消息说指定的值超出范围".



And the message says that "The specified value is out of range"

我对ColorTranslator一无所知. 另一方面,不支持msoCTrue.请参阅: MsoTriState枚举(Microsoft.Office.Core) [ ^ ]

这应该起作用:
I have no idea about ColorTranslator.
On the other side msoCTrue is not supported. Please see: MsoTriState enumeration (Microsoft.Office.Core)[^]

This should works:
Dim oDoc As Document = Nothing
'later
oDoc = ActiveDocument

    With oDoc
        .Background.Fill.ForeColor.RGB = RGB(128, 128, 0)
        .Background.Fill.Visible = msoTrue
        .Background.Fill.Solid
    End With
    
oDoc = Nothing