将构建块中的表插入到word文档中的指定位置。

时间:2015-12-09 14:00:01

标签: vba excel-vba word-vba excel

我想插入一个定义为构建块的表。我将内容控件放在文档中的指定位置,并通过“selectcontetcontrolsbytag”引用它。不幸的是,当表被插入到conentcontrol时,它被转换为RichText。这是我的代码:

ThisDocument.SelectContentControlsByTag("TermsConditions").Item(1).Range = _
ActiveDocument.AttachedTemplate.BuildingBlockTypes.Item(wdTypeTables).Categories.Item("Terms and Conditions Translation").BuildingBlocks.Item("Terms and Conditions Eng")

您能否帮助我使用正确的代码在指定位置插入构建块。另外,当用户从userform,combobox等中选择其他项目时,我希望这个构建块被另一个替换。

2 个答案:

答案 0 :(得分:1)

我的问题的完整解决方案是:

  1. Cindy Meister提出的解决方案替换内容中的内容 控制:
  2. 要更改内容控件“TermsConditions”中的内容,我添加了以下代码:

    如果doc.SelectContentControlsByTag(“TermsConditions”)。Item(1).Range.Text<> doc.SelectContentControlsByTag(“TermsConditions”)。Item(1).PlaceholderText Then doc.SelectContentControlsByTag( “TermsConditions”)。(1)项.Range.Cut 其他 结束如果

答案 1 :(得分:0)

我不确定你的意思"它被转换为富文本" ...

插入BuildingBlock的可接受方法是使用传递目标Range对象的BuildingBlock.Insert方法。例如(基于您的代码示例):

Dim doc as Word.Document
Dim rngTarget as Word.Range
Set doc = ActiveDocument
Set rngTarget = doc.SelectContentControlsByTag("TermsConditions").Item(1).Range 
doc.AttachedTemplate.BuildingBlockTypes.Item(wdTypeTables).Categories.Item("Terms and and Conditions Translation").BuildingBlocks.Item("Terms and Conditions Eng").Insert rngTarget, true

查看VBA语言参考中的示例...

此外,您不应在代码中使用ThisDocument,请使用ActiveDocument。 (更好的是,声明一个Document对象,为其分配ActiveDocument,然后使用它。)