Word 中的 VBA:以编程方式添加具有样式的内容控件 [英] VBA in Word: Programmatically add content control with a style

查看:122
本文介绍了Word 中的 VBA:以编程方式添加具有样式的内容控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 VBA 以编程方式将富文本内容控件添加到 Word .docm 时,有没有办法为内容设置样式?

When programatically adding a rich text content control to a Word .docm using VBA, is there a way to set a style for the contents?

作为比较,如果我使用 Word Developer 工具栏手动创建内容控件,我可以在内容控件的属性对话框中选择使用样式设置内容格式".我想要的结果和我那样做的一样,只是我需要用代码来做.

As a comparison, if I create a content control manually using the Word Developer toolbar, I can select "Use a style to format contents" in the properties dialog for the content control. The result I want is the same as if I did it that way, except I need to do it in code.

这是我添加内容控件的代码,它由单击命令按钮触发,该按钮还执行其他一些操作:

Here's the code I have that adds the content control, it's triggered by a command button click that does a few other things as well:

Private Sub selConcept_Click()

    ActiveDocument.InlineShapes(1).Delete
    ActiveDocument.InlineShapes(3).Delete
    ActiveDocument.InlineShapes(3).Delete

    Dim oCC As ContentControl
    Set oCC = ActiveDocument.ContentControls.Add(wdContentControlRichText, _
              Selection.Range)
    oCC.SetPlaceholderText , , "My placeholder text is here."
    oCC.Title = "Concept"
End Sub

推荐答案

如果你已经创建了样式,你可以像这样分配它:

If you already created the style, you can just assign it like this:

oCC.DefaultTextStyle = "style_name"

现在,如果没有,您必须先添加样式.类似的东西:

Now, if not, you'll have to add your style first. Something like:

ActiveDocument.Styles.Add Name:="MyStyle1", Type:=wdStyleTypeCharacter
With ActiveDocument.Styles("MyStyle1").Font
    .Name = "Arial"
    .Size = 12
    .Bold = True
    .Color = RGB(255, 0, 0) 'you can use RGB here
End With

oCC.DefaultTextStyle = "MyStyle1"

这篇关于Word 中的 VBA:以编程方式添加具有样式的内容控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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