使用 vba 插入/编辑 Header/Footer - Header 之后的额外段落字符和来自快速零件库的页脚
Inserting / editing Header/Footer using vba - extra Paragraph Character After Header and Footer from Quick Part Gallery
如果我从快速部件库中插入 header 或页脚,它通常会在下一行添加一个独立的段落字符。
这非常烦人,因为每次都需要清理。有没有办法防止这种行为?快速零件库中的一些默认 header 不会执行此操作。我自己创建的那些 - header 和页脚。
我目前有一个 VBA 宏,可以自动将所有这些 header 和页脚添加到目录中的文档中,但是当我必须离开时它对我没有多大用处无论如何,为每个文档点击删除两次。我可以通过脚本(仅适用于 header)找到并替换段落标记 (^p^p),但这样做会去除 header 中的样式。如果可以的话,我宁愿这些不属于快速部分。当我保存零件时,它们不在那里。有什么想法吗?
我回答了为什么段落标记出现在 your question posted on Super User 中的部分。此响应是针对使用 vba 插入积木块的地址。但是,您的 vba 不会导致额外的段落标记。正如超级用户中的回复所述,这是由于构建块的内容。
如果您的 vba(未显示)打开页眉或页脚区域并粘贴内容,则 Word 中的错误会将原始段落标记保留为额外内容。但是,如果您通过下面显示的过程之一使用它,则不应使用它。
录制的宏很少会达到您想要的效果,尤其是在共享模板时。
编写宏
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上的页面。
这是从我的 . That answer was helpfully edited by Timothy Rylatt.
几乎逐字复制的
如果我从快速部件库中插入 header 或页脚,它通常会在下一行添加一个独立的段落字符。
这非常烦人,因为每次都需要清理。有没有办法防止这种行为?快速零件库中的一些默认 header 不会执行此操作。我自己创建的那些 - header 和页脚。
我目前有一个 VBA 宏,可以自动将所有这些 header 和页脚添加到目录中的文档中,但是当我必须离开时它对我没有多大用处无论如何,为每个文档点击删除两次。我可以通过脚本(仅适用于 header)找到并替换段落标记 (^p^p),但这样做会去除 header 中的样式。如果可以的话,我宁愿这些不属于快速部分。当我保存零件时,它们不在那里。有什么想法吗?
我回答了为什么段落标记出现在 your question posted on Super User 中的部分。此响应是针对使用 vba 插入积木块的地址。但是,您的 vba 不会导致额外的段落标记。正如超级用户中的回复所述,这是由于构建块的内容。
如果您的 vba(未显示)打开页眉或页脚区域并粘贴内容,则 Word 中的错误会将原始段落标记保留为额外内容。但是,如果您通过下面显示的过程之一使用它,则不应使用它。
录制的宏很少会达到您想要的效果,尤其是在共享模板时。
编写宏
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上的页面。
这是从我的