Word VBA 插入一个 table 的内容积木
Word VBA Insert a table of contents building block
我正在尝试使用定义 table 内容的内置构建块之一将 table 内容插入 Word 文档。当我录制宏并插入 table 内容时,宏录制器会给出这行代码:
Application.Templates( _
"C:\Users\me\AppData\Roaming\Microsoft\Document Building Blocks33\Built-In Building Blocks.dotx" _
).BuildingBlockEntries("Automatic Table 1").Insert Where:=Selection.Range, RichText:=True
我已验证此名称 – Automatic Table 1 – 存在于构建块管理器中。当然,当我使用菜单栏功能区按钮插入内容的 table 时,内容的 table 确实插入正确。
但是,当我将同一行代码放入 VBA 宏中时,我收到一条错误消息,指出所请求的项目不存在。是否可以从 VBA 代码中引用构建块项目?谁能告诉我我做错了什么,或者如何实现我的目标?谢谢。
宏录制器只是一个起点。您发布的示例取决于构建块模板的非常具体的路径,其中包括您的用户名和您使用的语言(1033 是 U.S。英语)和 Word 版本(Word 2016 为 16和 2019 年)。此外,积木模板位置没有 VBA 快捷方式。
更可靠的方法是将 table 插入到您的宏模板或基于该模板的文档中。 Select table,然后选择 插入 > 快速部件 > 自动图文集 > 将 Selection 保存到自动图文集库 。您可以将名称设置为您喜欢的任何名称。将图库设置为 Table 的内容。确定,然后删除 table 样本。
现在您可以像这样使用更简单、更可靠的代码:
ActiveDocument.AttachedTemplate.BuildingBlockEntries("TOC1").Insert Where:=Selection.Range, RichText:=True
Word 不会在启动时加载构建基块,它们是按需加载的。当您单击其中一个 Building Block 库时,您可能会注意到在显示库之前有一个短暂的停顿,这是 Word 正在加载它们。
您可以通过在代码中使用 Application.Templates.LoadBuildingBlocks
安全地指示 Word 加载 Building Blocks,如果它们已经加载,则不会生成错误。
内置 Building Blocks 的路径也只对您有效,但您可以通过使用 Environ
到 return 路径的第一部分来解决这个问题。
Sub InsertTOC()
Dim path As String
Application.Templates.LoadBuildingBlocks
path = Environ$("USERPROFILE") & "\AppData\Roaming\Microsoft\Document Building Blocks33\Built-In Building Blocks.dotx"
Application.Templates(path).BuildingBlockEntries("Automatic Table 1").Insert Where:=Selection.Range, RichText:=True
End Sub
录制的宏很少会达到您想要的效果,尤其是在共享模板的情况下。
编写宏
To do this, you need to know:
The name of the building block
The name (and location) of the template that holds the building block unless the macro is in the same template
How to insert a macro.
See Installing Macros and Install/Employ VBA Procedures (Macros).
Building Block Name = "MyBB" (example in this macro, change to fit)
Situation 1 and 1a have the Building Block and the macro in the same template.
This simplifies coding because a macro can always tell
the name and location of the template that holds it. That information
is required to use a macro to insert a building block.
情况 1 - 模板同时包含构建块和宏
Here is the macro to insert that uniquely-named building block at the insertion point in the document:
Sub InsertMyBB()
' Will not work if there are multiple building blocks with the same name in the template! See below.
'
Dim sBBName As String
sBBName = "MyBB"
On Error GoTo Oops
Application.Templates.LoadBuildingBlocks ' Thank you Timothy Rylatt!
Application.Templates(ThisDocument.FullName).BuildingBlockEntries(sBBName).Insert _
Where:=Selection.Range, _
RichText:=True ' Insert MyBB Building Block
Exit Sub ' We're done here
Oops: ' Didn't work - building block not there!
MsgBox Prompt:="The Building Block " & sBBName & " cannot be found in " & _
ThisDocument.Name & ".", Title:="Didn't Work!"
On Error GoTo 0
End Sub
This and the following macro are both contained in a demonstration template that can be downloaded from my downloads page.
Situation 1a - template holding building blocks and macro in same template - multiple building blocks with the same name
In this situation, the previous macro would confuse Word and give unpredictable (to the user) results. In this case, the macro needs to
know both the gallery and category of the building block. The
following macro assumes that the building block is stored in the
AutoText gallery and in the General category. You can find the name of
the gallery and category using the Building Blocks Organizer. Category
names are plain text. Galleries are referenced in vba as Building
Block Types and use constants. You can find a list of the constants
for the different galleries here.
Sub InsertMyBB()
'
' Assumes that the Building Block is of the type AutoText (wdTypeAutoText) in Category "General"
' See https://msdn.microsoft.com/en-us/library/bb243303(v=office.12).aspx
'
' This is based in part upon contributions from Greg Maxey and Jay Freedman - any errors remain mine
' Written by Charles Kenyon February 2016
'
Dim sBBName As String
Dim sTempName As String
Dim oBB As BuildingBlock
sBBName = "MyBB" 'use the name of your building block instead of "MyBB"
sTempName = ThisDocument.FullName ' puts name and full path of template in string variable
On Error Resume Next
Application.Templates.LoadBuildingBlocks ' thank you Timothy Rylatt
Set oBB = Application.Templates(sTempName).BuildingBlockTypes(wdTypeAutoText) _
.Categories("General").BuildingBlocks(sBBName)
If Err.Number = 0 Then
oBB.Insert Selection.Range, True
Else
MsgBox Prompt:="The Building Block '" & sBBName & "' cannot be found in " & _
ThisDocument.Name & ".", Title:="Didn't Work!"
End If
On Error GoTo 0
lbl_Exit:
Exit Sub
End Sub
This and the preceding macro are both contained in a demonstration template that can be downloaded from my downloads page.
情况 2 - 包含构建基块的模板位于 Word 启动文件夹中并命名为 MyBBTemplate.dotx
This template, for some reason, does not hold the macro, it is in a separate template. We know the name of the container template. The
name of the template containing the macro does not matter for our
purposes.
Sub InsertMyBB()
' Will not work if the Startup Folder is the root directory of a drive, i.e. C:\
' For use with building block stored in a template loaded in the Word Startup Folder that does NOT hold this macro
' Will not work if there are multiple building blocks with the same name in the template!
'
Dim sBBName As String
Dim sTemplateName as String
Dim sStartupPath as String
sBBName = "MyBB"
sTemplateName="MyBBTemplate.dotx"
sStartupPath = Application.Options.DefaultFilePath(wdStartupPath)
On Error GoTo Oops ' error handler
Application.Templates.LoadBuildingBlocks ' thank you Timothy Rylatt
Application.Templates(sStartupPath & "\" & sTemplateName).BuildingBlockEntries(sBBName) _
.Insert Where:=Selection.Range, RichText:=True ' Insert MyBB Building Block
Exit Sub ' We're done here
Oops: ' Didn't work - building block not there!
MsgBox Prompt:="The Building Block " & sBBName & " cannot be found in " & _
sTemplateName".", Title:="Didn't Work!"
On Error GoTo 0
End Sub
情况3-模板持有积木在积木存放位置为“建筑Blocks.dotx”
This template also does not hold the macro because templates in the building blocks folder do not contribute macros to Word even if
they contain them. This macro incorporates code from Greg Maxey and
Jay Freedman given in this thread. Building Blocks.dotx is the name of
the template used, by default, to store custom building blocks other
than AutoText. It is stored, by user, in a language-dependent,
version-dependent folder. This macro is intended to retrieve the
building block regardless of the language or version.
Sub InsertMyBB()
' Based on code by Greg Maxey and Jay Freedman
' For use with building block stored in the default custom building blocks file "Building Blocks.dotx"
' Will not work if there are multiple building blocks with the same name in the template!
'
Templates.LoadBuildingBlocks ' in case building blocks not yet accessed
Dim sBBName As String
Dim sStartupPath as String
Dim sTemplateName as String
sBBName = "MyBB"
sTemplateName="Building Blocks.dotx"
sStartupPath = Application.Options.DefaultFilePath(wdStartupPath)
On Error GoTo Oops ' error handler
Application.Templates(sStartupPath & "\" & sTemplateName).BuildingBlockEntries(sBBName) _
.Insert Where:=Selection.Range, RichText:=True ' Insert MyBB Building Block
Exit Sub ' We're done here
Oops: ' Didn't work - building block not there!
MsgBox Prompt:="The Building Block " & sBBName & " cannot be found in " & _
sTemplateName & ".", Title:="Didn't Work!"
On Error GoTo 0
End Sub
各种插入积木的代码来自我在AutoText, Building Blocks and AutoFormat上的页面。
我正在尝试使用定义 table 内容的内置构建块之一将 table 内容插入 Word 文档。当我录制宏并插入 table 内容时,宏录制器会给出这行代码:
Application.Templates( _
"C:\Users\me\AppData\Roaming\Microsoft\Document Building Blocks33\Built-In Building Blocks.dotx" _
).BuildingBlockEntries("Automatic Table 1").Insert Where:=Selection.Range, RichText:=True
我已验证此名称 – Automatic Table 1 – 存在于构建块管理器中。当然,当我使用菜单栏功能区按钮插入内容的 table 时,内容的 table 确实插入正确。
但是,当我将同一行代码放入 VBA 宏中时,我收到一条错误消息,指出所请求的项目不存在。是否可以从 VBA 代码中引用构建块项目?谁能告诉我我做错了什么,或者如何实现我的目标?谢谢。
宏录制器只是一个起点。您发布的示例取决于构建块模板的非常具体的路径,其中包括您的用户名和您使用的语言(1033 是 U.S。英语)和 Word 版本(Word 2016 为 16和 2019 年)。此外,积木模板位置没有 VBA 快捷方式。
更可靠的方法是将 table 插入到您的宏模板或基于该模板的文档中。 Select table,然后选择 插入 > 快速部件 > 自动图文集 > 将 Selection 保存到自动图文集库 。您可以将名称设置为您喜欢的任何名称。将图库设置为 Table 的内容。确定,然后删除 table 样本。
现在您可以像这样使用更简单、更可靠的代码:
ActiveDocument.AttachedTemplate.BuildingBlockEntries("TOC1").Insert Where:=Selection.Range, RichText:=True
Word 不会在启动时加载构建基块,它们是按需加载的。当您单击其中一个 Building Block 库时,您可能会注意到在显示库之前有一个短暂的停顿,这是 Word 正在加载它们。
您可以通过在代码中使用 Application.Templates.LoadBuildingBlocks
安全地指示 Word 加载 Building Blocks,如果它们已经加载,则不会生成错误。
内置 Building Blocks 的路径也只对您有效,但您可以通过使用 Environ
到 return 路径的第一部分来解决这个问题。
Sub InsertTOC()
Dim path As String
Application.Templates.LoadBuildingBlocks
path = Environ$("USERPROFILE") & "\AppData\Roaming\Microsoft\Document Building Blocks33\Built-In Building Blocks.dotx"
Application.Templates(path).BuildingBlockEntries("Automatic Table 1").Insert Where:=Selection.Range, RichText:=True
End Sub
录制的宏很少会达到您想要的效果,尤其是在共享模板的情况下。
编写宏
To do this, you need to know:
The name of the building block
The name (and location) of the template that holds the building block unless the macro is in the same template
How to insert a macro. See Installing Macros and Install/Employ VBA Procedures (Macros).
Building Block Name = "MyBB" (example in this macro, change to fit)
Situation 1 and 1a have the Building Block and the macro in the same template. This simplifies coding because a macro can always tell the name and location of the template that holds it. That information is required to use a macro to insert a building block.
情况 1 - 模板同时包含构建块和宏
Here is the macro to insert that uniquely-named building block at the insertion point in the document:
Sub InsertMyBB()
' Will not work if there are multiple building blocks with the same name in the template! See below.
'
Dim sBBName As String
sBBName = "MyBB"
On Error GoTo Oops
Application.Templates.LoadBuildingBlocks ' Thank you Timothy Rylatt!
Application.Templates(ThisDocument.FullName).BuildingBlockEntries(sBBName).Insert _
Where:=Selection.Range, _
RichText:=True ' Insert MyBB Building Block
Exit Sub ' We're done here
Oops: ' Didn't work - building block not there!
MsgBox Prompt:="The Building Block " & sBBName & " cannot be found in " & _
ThisDocument.Name & ".", Title:="Didn't Work!"
On Error GoTo 0
End Sub
This and the following macro are both contained in a demonstration template that can be downloaded from my downloads page.
Situation 1a - template holding building blocks and macro in same template - multiple building blocks with the same name
In this situation, the previous macro would confuse Word and give unpredictable (to the user) results. In this case, the macro needs to know both the gallery and category of the building block. The following macro assumes that the building block is stored in the AutoText gallery and in the General category. You can find the name of the gallery and category using the Building Blocks Organizer. Category names are plain text. Galleries are referenced in vba as Building Block Types and use constants. You can find a list of the constants for the different galleries here.
Sub InsertMyBB()
'
' Assumes that the Building Block is of the type AutoText (wdTypeAutoText) in Category "General"
' See https://msdn.microsoft.com/en-us/library/bb243303(v=office.12).aspx
'
' This is based in part upon contributions from Greg Maxey and Jay Freedman - any errors remain mine
' Written by Charles Kenyon February 2016
'
Dim sBBName As String
Dim sTempName As String
Dim oBB As BuildingBlock
sBBName = "MyBB" 'use the name of your building block instead of "MyBB"
sTempName = ThisDocument.FullName ' puts name and full path of template in string variable
On Error Resume Next
Application.Templates.LoadBuildingBlocks ' thank you Timothy Rylatt
Set oBB = Application.Templates(sTempName).BuildingBlockTypes(wdTypeAutoText) _
.Categories("General").BuildingBlocks(sBBName)
If Err.Number = 0 Then
oBB.Insert Selection.Range, True
Else
MsgBox Prompt:="The Building Block '" & sBBName & "' cannot be found in " & _
ThisDocument.Name & ".", Title:="Didn't Work!"
End If
On Error GoTo 0
lbl_Exit:
Exit Sub
End Sub
This and the preceding macro are both contained in a demonstration template that can be downloaded from my downloads page.
情况 2 - 包含构建基块的模板位于 Word 启动文件夹中并命名为 MyBBTemplate.dotx
This template, for some reason, does not hold the macro, it is in a separate template. We know the name of the container template. The name of the template containing the macro does not matter for our purposes.
Sub InsertMyBB()
' Will not work if the Startup Folder is the root directory of a drive, i.e. C:\
' For use with building block stored in a template loaded in the Word Startup Folder that does NOT hold this macro
' Will not work if there are multiple building blocks with the same name in the template!
'
Dim sBBName As String
Dim sTemplateName as String
Dim sStartupPath as String
sBBName = "MyBB"
sTemplateName="MyBBTemplate.dotx"
sStartupPath = Application.Options.DefaultFilePath(wdStartupPath)
On Error GoTo Oops ' error handler
Application.Templates.LoadBuildingBlocks ' thank you Timothy Rylatt
Application.Templates(sStartupPath & "\" & sTemplateName).BuildingBlockEntries(sBBName) _
.Insert Where:=Selection.Range, RichText:=True ' Insert MyBB Building Block
Exit Sub ' We're done here
Oops: ' Didn't work - building block not there!
MsgBox Prompt:="The Building Block " & sBBName & " cannot be found in " & _
sTemplateName".", Title:="Didn't Work!"
On Error GoTo 0
End Sub
情况3-模板持有积木在积木存放位置为“建筑Blocks.dotx”
This template also does not hold the macro because templates in the building blocks folder do not contribute macros to Word even if they contain them. This macro incorporates code from Greg Maxey and Jay Freedman given in this thread. Building Blocks.dotx is the name of the template used, by default, to store custom building blocks other than AutoText. It is stored, by user, in a language-dependent, version-dependent folder. This macro is intended to retrieve the building block regardless of the language or version.
Sub InsertMyBB()
' Based on code by Greg Maxey and Jay Freedman
' For use with building block stored in the default custom building blocks file "Building Blocks.dotx"
' Will not work if there are multiple building blocks with the same name in the template!
'
Templates.LoadBuildingBlocks ' in case building blocks not yet accessed
Dim sBBName As String
Dim sStartupPath as String
Dim sTemplateName as String
sBBName = "MyBB"
sTemplateName="Building Blocks.dotx"
sStartupPath = Application.Options.DefaultFilePath(wdStartupPath)
On Error GoTo Oops ' error handler
Application.Templates(sStartupPath & "\" & sTemplateName).BuildingBlockEntries(sBBName) _
.Insert Where:=Selection.Range, RichText:=True ' Insert MyBB Building Block
Exit Sub ' We're done here
Oops: ' Didn't work - building block not there!
MsgBox Prompt:="The Building Block " & sBBName & " cannot be found in " & _
sTemplateName & ".", Title:="Didn't Work!"
On Error GoTo 0
End Sub
各种插入积木的代码来自我在AutoText, Building Blocks and AutoFormat上的页面。