使用VBA以编程方式将富文本内容控件添加到Word .docm时,有没有办法为内容设置样式?
作为比较,如果我使用Word Developer工具栏手动创建内容控件,我可以选择"使用样式来格式化内容"在内容控件的属性对话框中。我想要的结果就像我这样做的那样,除了我需要在代码中完成它。
这里有我添加内容控件的代码,它由一个命令按钮点击触发,还有一些其他的东西:
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
答案 0 :(得分:1)
如果您已经创建了样式,则可以像这样分配:
oCC.DefaultTextStyle = "style_name"
现在,如果没有,您必须先添加样式。类似的东西:
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"