0

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

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

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

where<color>等于内容控件,“this”为粗体。显然该.Text属性在这里不起作用,但我不知道如何使用内容控件插入此字符串并在“this”一词上设置格式。

有什么建议吗?

4

1 回答 1

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

As for the content control, it depends on what you mean. If you just need to insert a control with the Title or Tag "color", the above would be enough. But if you have controls connected to a custom XML data store, then you would need to use .XmlMapping.SetMapping to set its Xpath to point to the correct item in the store.

于 2013-08-21T09:43:41.110 回答