简体   繁体   English

如何在 Word 样式定义中包含 BackgroundPatternColor 或 HighlightColor?

[英]How to include BackgroundPatternColor or HighlightColor in a Word Style definition?

Since the Highlight color palette in Ms-Office/Word is very limited, in some Word documents we use BackgroundPatternColor to give the background color of the text.由于 Ms-Office/Word 中的 Highlight 调色板非常有限,因此在某些 Word 文档中,我们使用 BackgroundPatternColor 来给出文本的背景颜色。

Question:题:
I would like to define a custom Style as a spinoff of the Normal Style, that includes a custom background color.我想将自定义样式定义为普通样式的衍生,其中包括自定义背景颜色。
In the Style definition dialogue I did not find any option where I can include the bgcolor of the selection.在样式定义对话框中,我没有找到任何可以包含选择的 bgcolor 的选项。 Any ideas?有任何想法吗?

Note: The only option close to that is the so called Border>Shading>Fill Color, but that always spans to the full line/paragraph, cannot be applied to less.注意:与此接近的唯一选项是所谓的“边框”>“阴影”>“填充颜色”,但它始终跨越整行/段落,不能应用于更少。

You can change the background color, but if you don't want it coloring the entire paragraph then you must create a character style and set the color for its .Font property.您可以更改背景颜色,但如果您不希望它为整个段落着色,那么您必须创建一个字符样式并为其.Font属性设置颜色。

(Note: you could also create a so-called "linked" style, that can be use for both paragraph and font formatting, but these are generally frowned upon in professional documentation circles.) (注意:您也可以创建一个所谓的“链接”样式,可用于段落和字体格式,但这些在专业文档圈中通常不受欢迎。)

Here's an example这是一个例子

Sub ChangeStyleColor()
    Dim styl As word.style
    Dim stylName As String
    Dim color As word.WdColor

    stylName = "fontBlueBackground"
    color = wdColorAqua

    ' the style might not exist - if not, create it
    On Error Resume Next
    Set styl = ActiveDocument.styles(stylName)
    On Error GoTo 0

    If styl Is Nothing Then
        Set styl = ActiveDocument.styles.Add(stylName, word.WdStyleType.wdStyleTypeCharacter)
        styl.BaseStyle = word.WdBuiltinStyle.wdStyleDefaultParagraphFont
    End If

    CharStyleBackgroundColor styl, color

End Sub

Sub CharStyleBackgroundColor(styl As word.style, color As word.WdColor)
    styl.Font.Shading.BackgroundPatternColor = color
End Sub

Until somebody comes up with an inbuilt answer直到有人提出一个内置的答案
as usual, I will use a workaround:像往常一样,我将使用一种解决方法:

I will use custom macros as custom Styles for such cases, and i will put these macros on the ribbon into an m-styles folder.对于这种情况,我将使用自定义宏作为自定义样式,并将这些宏放在功能区上的 m-styles 文件夹中。
For example:例如:

Sub F_HáttérSzín_Hupilila()
    Dim SzínKód As Variant
    SzínKód = 13444920
    Selection.Font.Shading.BackgroundPatternColor = SzínKód
End Sub

通过右键单击并添加背景来添加背景颜色

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

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