【问题标题】:Word Range with Content Control带有内容控制的词范围
【发布时间】:2018-12-22 04:26:02
【问题描述】:

对不起,如果这是一个如此简单的问题,但对于我的生活,我无法弄清楚如何做某事。

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

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

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

有什么建议吗?

【问题讨论】:

  • 我不确定您所说的 &lt;color&gt; 作为内容控件是什么意思。你想在那里插入contentcontrol 或者你想在其他内容控件中添加一个文本?
  • 是的,&lt;color&gt; 位置应该是一个内容控件,比如纯文本控件。

标签: vba ms-word word-contentcontrol


【解决方案1】:

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

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 设置为指向存储中的正确项目。

【讨论】:

  • 哇,这太完美了。 wdCollapseEnd 范围的部分我还不知道它是什么意思,但这有效。我会研究并了解更多。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-03
  • 2013-11-29
  • 1970-01-01
  • 1970-01-01
  • 2011-07-22
  • 1970-01-01
相关资源
最近更新 更多