VBA写入Word,更改字体格式 [英] VBA Writing to Word, changing font formatting

查看:878
本文介绍了VBA写入Word,更改字体格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Excel编写VBA脚本,以将基于某些表的文本输出到Word文档.在大多数情况下,一切工作都很好(在我从stackoverflow获得大量帮助的同时,我正在自学).我有一个相当长的代码,因此很难将其全部复制到这里,我将尝试展示相关的部分.

I'm writing a VBA script in Excel to output text based on some tables to a Word document. For the most part everything is working out beautifully (I'm teaching myself as I go with lots of help from stackoverflow). I've got a rather long code so copying it all here would be difficult, I'm going to try and show the pertinent parts.

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

The issue I'm running into is in trying to change the formatting of my font as I go through. I've created a few different Styles to assist in standardizing the formatting options,

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

然后,我正在尝试执行此操作,其中输出文本在句子更改格式的中间将有一个单词,

Then, I'm trying to do this, where the output text will have a single word in the middle of the sentence change format,

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

但是我得到的是该单词的格式没有变化的句子.

But what I get is the sentence without the change if format for that word.

更奇怪的是,我还有一些组件应将我的粗体"样式应用于整个句子,其中一个的确适用于粗体,另一个则不适用.

More curiously, I also have a few components which should apply my "Bold" style to an entire sentence, one of which does apply the Bold, the other which does not,

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

其中一些选择,但我不知道该怎么做.我可以用它来完成文档的范围"操作,但这最终将其应用于文档的现有部分,而不应用于任何新文本.

to some of these Selections, but I can't figure out how do do that. I can get it to do it for a "Range" to the Document, but that ends up applying it to the existing sections of the document, but not to any of the new text.

推荐答案

您遇到的问题之一是,在创建样式时,默认情况下它是段落样式(或者在Word的最新版本中,通常是链接样式",有时被称为段落/字符样式".

One of the problems that you have is that when you create a Style, by default it is a Paragraph style (or in recent versions of Word, it will typically be a "Linked style", which are sometimes thought of as Paragraph/Character Styles).

所以当您执行

.Style = wrdDoc.Styles("NoFormat")

例如,第二次将样式应用于整个段落.您忘记对段落的一部分应用未知"这一事实被遗忘了.

the second time, for example, the style is applied to the whole paragraph. The fact that you tried to apply "Unknown" to part of the paragraph is forgotten.

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

If you create the same test styles as character styles, e.g.

.Styles.Add "NoFormat", wdStyleType.wdStyleTypeCharacter

以及类似的未知"样式

并再次运行您的第一个测试示例,您应该看到区别.

and run your first test example again, you should see the difference.

这篇关于VBA写入Word,更改字体格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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