在Microsoft Word 2007宏中创建目录时应使用哪些VBA代码

在Microsoft Word 2007宏中创建目录时应使用哪些VBA代码,vba,ms-word,tableofcontents,Vba,Ms Word,Tableofcontents,我想在MicrosoftWord2007中定义一个宏,当按下热键时,该宏将插入具有所提供自动样式的目录。我成功地定义了一个宏来插入一个非样式(例如基本)目录,如下所示: Sub InsertTableOfContents() ' ' InsertTableOfContents Macro ' ' With ActiveDocument .TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:=

我想在MicrosoftWord2007中定义一个宏,当按下热键时,该宏将插入具有所提供自动样式的目录。我成功地定义了一个宏来插入一个非样式(例如基本)目录,如下所示:

Sub InsertTableOfContents()
'
' InsertTableOfContents Macro
'
'
    With ActiveDocument
        .TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _
            True, UseHeadingStyles:=True, UpperHeadingLevel:=1, _
            LowerHeadingLevel:=3, IncludePageNumbers:=True, AddedStyles:="", _
            UseHyperlinks:=True, HidePageNumbersInWeb:=True, UseOutlineLevels:= _
            True
        .TablesOfContents(1).TabLeader = wdTabLeaderDots
        .TablesOfContents.Format = wdIndexIndent
    End With
End Sub
Sub InsertStyledTOC()
'
' Macro to insert a table of contents styled like Automatic Table 2
'
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Automatic Table 2"). _
    Insert Where:=Selection.Range, RichText:=True
End Sub
但是,当我尝试插入样式化的目录时,如下所示:

Sub InsertTableOfContents()
'
' InsertTableOfContents Macro
'
'
    With ActiveDocument
        .TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _
            True, UseHeadingStyles:=True, UpperHeadingLevel:=1, _
            LowerHeadingLevel:=3, IncludePageNumbers:=True, AddedStyles:="", _
            UseHyperlinks:=True, HidePageNumbersInWeb:=True, UseOutlineLevels:= _
            True
        .TablesOfContents(1).TabLeader = wdTabLeaderDots
        .TablesOfContents.Format = wdIndexIndent
    End With
End Sub
Sub InsertStyledTOC()
'
' Macro to insert a table of contents styled like Automatic Table 2
'
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Automatic Table 2"). _
    Insert Where:=Selection.Range, RichText:=True
End Sub
我得到以下错误:

运行时错误5941请求的集合成员不存在

我认为这表明
BuildingBlockEntries
(例如自动表2)的引用成员不存在,但我不清楚为什么或如何更正它

谢谢你的帮助

编辑-我尝试按照建议使用应用程序默认构建块模板的文件路径:

Application.Templates(“C:\Program Files\Microsoft Office\Office12\Document Parts\1033\Building Blocks.dotx”).BuildingBlockEntries(“自动表2”)。插入其中:=Selection.Range_
,RichText:=True


但是,我仍然收到错误:
运行时错误5941请求的集合成员不存在

您的代码希望在附加的模板中找到构建块,如果您没有做任何特殊的操作,这可能是正常的。dotm。实际上,Microsoft将内置构建块存储在不同的模板中。如果录制宏,您将看到此模板的位置(我的位于“C:\Users\owner\AppData\Roaming\Microsoft\Document Building Blocks\1033\14\Build in Building Blocks.dotx”中)

所以,你有两个选择。您可以使用Templates集合访问该模板,并从那里插入构建块(宏记录器是您的朋友)。或者,您可以将构建块保存到Normal.dotm,以使访问它更容易。要执行此操作,请单击“插入>快速文本>构建块”,在列表中找到构建块,编辑其属性,然后将其保存为“普通”。如果你这样做,你的代码应该可以工作(我有2010年,但我打赌这是非常相似的)

我不知道这两种选择之间有什么真正的区别,假设这只是为了你,而不是你需要分发的东西

编辑以添加我从宏录制器获得的代码:

    Application.Templates( _
    "C:\Users\owner\AppData\Roaming\Microsoft\Document Building Blocks\1033\14\Built-In Building Blocks.dotx" _
    ).BuildingBlockEntries("Automatic Table 2").Insert Where:=Selection.Range _
    , RichText:=True

因此,您应该尝试用它替换InsertStyledTOC中的代码。

是TOC
选项。范围
?那么,您是否只是在尝试将样式应用于选择范围?你知道什么是
BuildingBlockEntries
?(我不知道!)。尝试在即时窗口中输入此项(在VBA编辑器中按Ctrl+G):
对于i=1的activedocument.AttachedTemplate.buildingblockentries.count:debug.Print activedocument.AttachedTemplate.buildingblockentries(i)。名称:下一个i
,您是否尝试过录制自己的宏以执行所需样式TOC的插入并检查代码?上面的代码是录制的宏的结果,在录制的宏中,我从“参考->目录”菜单中选择了目录(自动表2)。我还没有尝试输入调试代码,所以我将尝试一下。很抱歉,对于vba和宏来说有点陌生。没有必要道歉,每个人都有不同程度的经验。我对VBA这个词非常缺乏经验。我发表评论是因为其他人都没有,只是想给你一些调试的想法。看来Christina终于有了这个谢谢你的帮助。宏中记录的哪些功能需要生成的vba代码引用内置构建块的位置?实际上,我已经找到了该文件,
构建块
的位置,但我不确定如何使用templates集合访问它,能否提供一个示例?