Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Word VBA插入构建基块_Vba_Ms Word - Fatal编程技术网

使用Word VBA插入构建基块

使用Word VBA插入构建基块,vba,ms-word,Vba,Ms Word,我正在尝试插入一个已格式化的表,该表已保存在word中,名为“DV table”,作为使用VBA构建块的一部分。我需要将此表插入word文档的第13段。 下面是我的代码。前3行只是将选择设置为第12段,然后创建一个新的段落(13)。最后一行代码是插入表。但是当我运行这个时,它给出了错误 编译错误:未定义子或函数 我想这不是定义位置的正确方法。我想在这方面得到一些帮助。谢谢 ActiveDocument.Paragraphs(12).Range.Select Selection.EndKey Un

我正在尝试插入一个已格式化的表,该表已保存在word中,名为“DV table”,作为使用VBA构建块的一部分。我需要将此表插入word文档的第13段。 下面是我的代码。前3行只是将选择设置为第12段,然后创建一个新的段落(13)。最后一行代码是插入表。但是当我运行这个时,它给出了错误

编译错误:未定义子或函数

我想这不是定义位置的正确方法。我想在这方面得到一些帮助。谢谢

ActiveDocument.Paragraphs(12).Range.Select
Selection.EndKey Unit:=wdLine
Selection.Paragraphs.Add

 ActiveDocument.AttachedTemplate.BuildingBlockEntries("DV Table" _
      ).Insert Where:=Paragraphs(13).Range.Select, RichText:=True

Where
参数需要一个
范围
对象。
段落(13)有两个问题。范围。选择

  • 这是一种方法——它是一种动作,选择一些东西,而不是返回一个对象
  • 第(13)段
    不是“完全限定”-VBA不知道它是什么/它的意思
一种可能是

ActiveDocument.Paragraphs(13).Range
注意
ActiveDocument.
前面的
段落
:这个“完全限定”
段落(13)
-它告诉VBA它属于什么。而且,由于
其中
需要一个
范围
对象,
段落(13)。范围
应该是一个正确的“目标”(我没有测试过您的代码)

通常,最好不要使用
选择
,而只使用
范围
对象。通常不需要使用VBA进行实际选择。问题中代码片段的另一种选择是

Dim rng As Word.Range

Set rng = ActiveDocument.Paragraphs(13).Range
rng.Collapse wdCollapseEnd    'like pressing right-arrow for a selection
rng.InsertParagraphAfter
rng.Collapse wdCollapseStart  ' like pressing left-arrow for a selection
'rng.Select                   ' for testing / demo purposes
ActiveDocument.AttachedTemplate.BuildingBlockEntries("DV Table" _
               ).Insert Where:=rng, RichText:=True

在这种情况下,文档中的选择不会更改。没有屏幕闪烁;而且代码执行得更快。这种工作方式需要习惯,但一旦熟悉它,就更容易识别代码应该做什么<代码>选择
对于操作的内容相当模糊,特别是当有大量代码使用它时。

我建议您学习如何使用F1。将光标放置在关键字中,然后按F1键。这将打开该关键字的MS帮助页面。然后阅读帮助,特别注意返回类型(如果有的话)。你能帮我举一个例子吗?你还在努力解决这个问题吗?