VBA写入Word,更改字体格式

时间:2014-10-08 19:16:57

标签: vba excel-vba ms-word excel

我在Excel中编写VBA脚本,将基于某些表的文本输出到Word文档。在大多数情况下,一切都很美妙(我正在自学,因为我从stackoverflow获得了很多帮助)。我有一个相当长的代码,所以在这里复制它将很困难,我将尝试展示相关的部分。

我遇到的问题是在我经历时尝试更改字体的格式。 我已经创建了一些不同的样式来帮助标准化格式化选项,

With wrdDoc
    .Styles.Add ("SectionHeader")
    .Styles.Add ("NoFormat")
    .Styles.Add ("Marginal")
    .Styles.Add ("Failed")
    .Styles.Add ("Unknown")
    .Styles.Add ("Bold")
    With .Styles("SectionHeader")
        .Font.Name = "Calibri"
        .Font.Size = 12
        .Font.Underline = True
        .Font.Bold = False
        .Font.Italic = False
        .Font.Strikethrough = False
        .Font.Subscript = False
        .Font.Superscript = False
        .Font.Color = RGB(0, 0, 0)
    End With
    With .Styles("NoFormat")
        .Font.Name = "Calibri"
        .Font.Size = 12
        .Font.Underline = False
        .Font.Bold = False
        .Font.Italic = False
        .Font.Strikethrough = False
        .Font.Subscript = False
        .Font.Superscript = False
        .Font.Color = RGB(0, 0, 0)
    End With
    With .Styles("Marginal")
        .Font.Name = "Calibri"
        .Font.Size = 12
        .Font.Underline = False
        .Font.Bold = True
        .Font.Italic = False
        .Font.Strikethrough = False
        .Font.Subscript = False
        .Font.Superscript = False
        .Font.Color = RGB(0, 0, 255)
    End With
    With .Styles("Failed")
        .Font.Name = "Calibri"
        .Font.Size = 12
        .Font.Underline = True
        .Font.Bold = True
        .Font.Italic = False
        .Font.Strikethrough = False
        .Font.Subscript = False
        .Font.Superscript = False
        .Font.Color = RGB(255, 0, 0)
    End With
    With .Styles("Unknown")
        .Font.Name = "Calibri"
        .Font.Size = 12
        .Font.Underline = False
        .Font.Bold = True
        .Font.Italic = False
        .Font.Strikethrough = False
        .Font.Subscript = False
        .Font.Superscript = False
        .Font.Color = RGB(0, 176, 80)
    End With
    With .Styles("Bold")
        .Font.Name = "Calibri"
        .Font.Size = 12
        .Font.Underline = False
        .Font.Bold = True
        .Font.Italic = False
        .Font.Strikethrough = False
        .Font.Subscript = False
        .Font.Superscript = False
        .Font.Color = RGB(0, 0, 0)
    End With
End With

然后,我正在尝试这样做,输出文本将在句子更改格式的中间有一个单词,

With wrdApp.Selection
    .Style = wrdDoc.Styles("NoFormat")
    .TypeText Text:="The start of this sentence is "
    .Style = wrdDoc.Styles("Unknown")
    .TypeText Text:="unknown"
    .Style = wrdDoc.Styles("NoFormat")
    .TypeText Text:=" so we will keep trying..."
    .TypeParagraph
End With

但我得到的是没有改变的句子,如果该词的格式。

更奇怪的是,我还有一些组件应该将我的“Bold”风格应用于整个句子,其中一个适用于Bold,另一个不应用,

With wrdApp.Selection
    .Style = wrdDoc.Styles("Bold")
    .TypeText Text:="This one doesn't work"
End With

With wrdApp.Selection
    .Style = wrdDoc.Styles("Bold")
    .TypeText Text:="this one works!"
    .TypeParagraph
End With

我也希望能够申请

    .ParagraphFormat.SpaceAfter = 0
    .ParagraphFormat.SpaceBefore = 0

这些选择中的一些,但我无法弄清楚如何做到这一点。我可以让它为文档的“范围”做到这一点,但最终将它应用于文档的现有部分,而不是任何新文本。

2 个答案:

答案 0 :(得分:1)

你遇到的一个问题是,当你创建一个Style时,默认它是一个Paragraph样式(或者在Word的最新版本中,它通常是一个“链接样式”,有时被认为是段落/字符样式)。

所以当你执行

.Style = wrdDoc.Styles("NoFormat")

第二次,例如,样式应用于整个段落。您试图将“未知”应用于段落的一部分这一事实被遗忘了。

如果您创建与字符样式相同的测试样式,例如

.Styles.Add "NoFormat", wdStyleType.wdStyleTypeCharacter

,类似于“未知”风格

再次运行你的第一个测试例,你应该看到差异。

答案 1 :(得分:0)

我总是对Word中的回合范围感到有点困惑,但尝试这样的事情:

Sub TestAdd()
    AddWithStyle "This is a normal ", "Normal"
    AddWithStyle "but this is different", "Heading 1"
    AddWithStyle " and this is back to normal ", "Normal"
End Sub


Sub AddWithStyle(sText As String, sStyle As String)
    Selection.TypeText sText
    Selection.MoveLeft unit:=wdCharacter, Count:=Len(sText), Extend:=wdExtend
    Selection.Style = ActiveDocument.Styles(sStyle)
    Selection.Collapse Direction:=wdCollapseEnd
End Sub
相关问题