设置页眉和页脚的背景颜色

问题描述 投票:0回答:1

我已经能够使用oSourceTable.Shading.BackgroundPatternColor = wdRed更改文档正文中Word表的背景颜色。但是,相同的代码不适用于页眉或页脚Word表。我也尝试过将“节”的颜色设置为背景色,但无济于事。该代码运行,但是页眉和页脚始终以白色背景显示。

我主要完成Excel VBA,只有一小部分Word VBA,所以也许我在这里遗漏了一些明显的东西。预先感谢您的任何想法和/或建议。

根据要求,这是我正在使用的代码。 vTableBackColors只是颜色的数组。该代码用于主体表,并且运行良好。

For Each oWordTable In oWordDoc.Tables
    lIndex = lIndex + 1
    oWordTable.Shading.BackgroundPatternColor = vTableBackColors(lIndex)
Next 

我试图对页眉和页脚表执行相同的操作,但是不起作用。我尝试如下使用页眉/页脚表。

For Each oWordSection In oWordDoc.Sections
    For Each oWordTable In oWordSection.Headers.Item(wdHeaderFooterPrimary).Range.Tables
        ' I selected this one to see if it would make a difference.
        oWordTable.Select
        oWordTable.Shading.BackgroundPatternColor = m_HeaderBackColor
    Next
    For Each oWordTable In oWordSection.Footers.Item(wdHeaderFooterPrimary).Range.Tables
        oWordTable.Shading.BackgroundPatternColor = m_FooterBackColor
    Next
Next

我也尝试过直接使用节的页眉/页脚。检查前后,表明BackgroundPatternColor已根据需要进行了更改,但未显示。

oWordSection.Headers.Item(wdHeaderFooterPrimary).Range.Shading.BackgroundPatternColor = m_HeaderBackColor

enter image description here

这里是屏幕截图,显示了已着色的正文表和未更改的标题。与正文表颜色不同,Word设计器可能从未显示过“页眉/页脚”颜色,而是仅在运行时显示了这种颜色吗?

感谢您提供其他信息。

vba ms-word word-vba
1个回答
0
投票

«我已经能够通过以下方式更改文档正文中Word表的背景颜色:oSourceTable.Shading.BackgroundPatternColor = red。»除非您将“ red”定义为RGB值,否则该方法将无效。

关于页眉/页脚问题,这对我有用:

For Each oWordSection In oWordDoc.Sections
    For Each oWordTable In oWordSection.Headers.Item(wdHeaderFooterPrimary).Range.Tables
        oWordTable.Shading.BackgroundPatternColorIndex = wdRed
    Next
    For Each oWordTable In oWordSection.Footers.Item(wdHeaderFooterPrimary).Range.Tables
        oWordTable.Shading.BackgroundPatternColorIndex = wdRed
    Next
Next

也许您的根本问题在于您分配给'm_HeaderBackColor'和'm_FooterBackColor'的值-您发布的代码不会显示这些值。您的代码也有可能解决了错误的页眉/页脚(即,可能不是您需要解决的主要页眉/页脚)。

© www.soinside.com 2019 - 2024. All rights reserved.