大佬教程收集整理的这篇文章主要介绍了使用 vba 插入/编辑页眉/页脚 - 来自 Quick Part Gallery 的页眉和页脚之后的额外段落字符,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
如果我从快速部件库中插入页眉或页脚,它通常会在下一行添加一个独立的段落字符。
这非常烦人,因为它每次都需要清理。有没有办法防止这种行为?快速部件库中的一些默认标题不执行此操作。我自己创建的那些 - 用于页眉和页脚。
我目前有一个 VBA 宏,可以自动将所有这些页眉和页脚添加到目录中的文档中,但是当我必须进入并为每个文档点击删除两次时,它对我没有多大好处。我可以通过脚本(仅在标题中有效)找到并替换段落标记 (^p^p),但这样做会从标题中删除样式。如果这是一个选项,我宁愿这些不是快速部分的一部分。当我保存零件时他们不在那里。有什么想法吗?
我回答了段落标记为什么会出现在 your question posted on Super User 中的部分。此响应是为了解决使用 vba 插入构建块的问题。但是,您的 vba 不会导致额外的段落标记。正如 Super User 中的响应所述,这是由于构建块的内容。
如果您的 vba(未显示)打开页眉或页脚区域并粘贴内容,则 Word 中的错误会将原始段落标记保留为额外标记。但是,如果您通过下面显示的过程之一使用它,则不应使用它。
录制的宏很少会执行您想要的操作,尤其是在您共享模板时。
要做到这一点,您需要知道:
积木名称
保存构建块的模板的名称(和位置),除非宏在同一个模板中
如何插入宏。 请参阅安装宏和安装/使用 VBA 过程(宏)。
Building Block Name = "MyBB"(此宏中的示例,更改以适应)
情况 1 和 1a 在同一模板中具有 Building Block 和宏。 这简化了编码,因为宏总是可以告诉 包含它的模板的名称和位置。那个信息 需要使用宏插入构建块。
情况 1 - 模板同时包含构建块和宏
这是在文档中的插入点插入唯一命名的构建块的宏:
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
这个和下面的宏都包含在一个演示模板中,可以从我的下载页面下载。
情况 1a - 模板在同一模板中包含构建块和宏 - 多个具有相同名称的构建块
在这种情况下,之前的宏会混淆 Word 并给出不可预测的(用户)结果。在这种情况下,宏需要 知道积木的画廊和类别。这 以下宏假定构建块存储在 自动图文集库和常规类别。你可以找到名字 使用 Building Blocks Organizer 的画廊和类别。类别 名称是纯文本。画廊在 vba 中被称为建筑 块类型和使用常量。您可以找到常量列表 对于这里不同的画廊。
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
这个和前面的宏都包含在一个演示模板中,可以从我的下载页面下载。
情况 2 - 包含构建块的模板位于 Word 启动文件夹中并命名为 MyBBTemplate.dotx
由于某种原因,这个模板不包含宏,它在一个单独的模板中。我们知道容器模板的名称。这 包含宏的模板名称对我们来说无关紧要 目的。
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
情况三——模板所在的积木存放位置为“Building Blocks.dotx”
这个模板也不包含宏,因为构建块文件夹中的模板不会向 Word 贡献宏,即使 它们包含它们。这个宏结合了 Greg Maxey 和 Jay Freedman 在此线程中给出。 Building Blocks.dotx 是 默认情况下用于存储自定义构建块的模板 比自动图文集。它由用户以依赖于语言的方式存储, 版本相关的文件夹。此宏旨在检索 构建块,无论语言或版本如何。
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 上的页面。
这几乎是从我的 earlier answer to a similar question 中逐字复制的。 Timothy Rylatt 对该答案进行了有益的编辑。
以上是大佬教程为你收集整理的使用 vba 插入/编辑页眉/页脚 - 来自 Quick Part Gallery 的页眉和页脚之后的额外段落字符全部内容,希望文章能够帮你解决使用 vba 插入/编辑页眉/页脚 - 来自 Quick Part Gallery 的页眉和页脚之后的额外段落字符所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。