带内容控件的单词范围



如果这是一个如此简单的问题,我很抱歉,但就我的一生而言,我不知道如何做某事。

我在Word中的表中有一个单元格,我想以编程方式在其中输入文本。在该文本中,我需要一个内容控件。因此,文本将如下所示:

mytable.Cell(1,).Range.Text = What <color> is **this**? 

其中<color>等于内容控件,"this"为粗体。显然,.Text属性在这里不起作用,但我不知道如何在单词"this"上插入带有内容控件和格式的字符串。

有什么建议吗?

我通常使用循序渐进的方法。。。

Dim cc As Word.ContentControl
Dim rng As Word.Range
Set rng = ActiveDocument.Tables(1).Cell(2, 3).Range
' Exclude the end of cell marker
rng.SetRange rng.Start, rng.End - 1
rng.Text = "What "
rng.Collapse direction:=Word.WdCollapseDirection.wdCollapseEnd
Set cc = rng.ContentControls.Add(wdContentControlText, rng)
With cc
  ' set what you need
  ' the following are just names.
  .Tag = "color"
  .Title = "color"
End With
' Word does not extend the range to include the CC
' We want to step beyond it
rng.SetRange cc.Range.End + 1, cc.Range.End + 1
Set cc = Nothing
rng.InsertAfter "is "
rng.Collapse direction:=WdCollapseDirection.wdCollapseEnd
rng.InsertAfter "this"
rng.Font.Bold = True
rng.Collapse direction:=WdCollapseDirection.wdCollapseEnd
rng.InsertAfter "?"
rng.Font.Bold = False
Set rng = Nothing

至于内容控制,这取决于你的意思。如果你只需要插入一个标题或标签为"颜色"的控件,以上就足够了。但是,如果控件连接到自定义XML数据存储,则需要使用.XmlMapping.SetMapping将其Xpath设置为指向存储中的正确项。

最新更新