如何以编程方式更改Word文档的背景颜色? [英] How Can I Change The Background Color Of A Word Document Programatically ?

查看:221
本文介绍了如何以编程方式更改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


这篇关于如何以编程方式更改Word文档的背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆