【问题标题】:Inserting / editing Header/Footer using vba - extra Paragraph Character After Header and Footer from Quick Part Gallery使用 vba 插入/编辑页眉/页脚 - Quick Part Gallery 中页眉和页脚后的额外段落字符
【发布时间】:2021-08-29 12:41:56
【问题描述】:

如果我从快速部件库中插入页眉或页脚,它通常会在下一行添加一个独立的段落字符。

这非常烦人,因为它每次都需要清理。有没有办法防止这种行为?快速部件库中的几个默认标题不这样做。我自己创建的那些 - 用于页眉和页脚。

我目前有一个 VBA 宏,可以自动将所有这些页眉和页脚添加到目录中的文档中,但是当我必须进入并为每个文档点击两次删除时,它对我没有多大好处。我可以通过脚本找到并替换段落标记(^p^p)(仅在标题中有效),但这样做会从标题中剥离样式。如果可以的话,我宁愿这些不是快速部分的一部分。当我保存零件时他们不在那里。有什么想法吗?

【问题讨论】:

  • 感谢您的回复,查尔斯。我将把它交给超级用户——但我也在寻找程序化解决方案。这样做的目的是让我的 VBA 脚本可以添加没有额外段落标记的快速部分,任何能让我到达那里的解决方案我都可以。至于快速部分,在保存之前是不包含标记的。我试过多次删除它们并重新保存快速部件。大多数预建的 Word 也这样做。
  • 您将同名的构建块存储在多个模板中。当您计划分发时,普通模板不适合存储它们。我建议查看模板的层次结构——使用哪个构建块。我提供的宏确实针对不同的位置。 addbalance.com/usersguide/templates.htm#hierarchy
  • 首先使用附加模板中的构建块。 -- 如果没有该名称,则使用普通模板中的该名称之一。 -- 如果在其中任何一个位置都没有找到该名称,则在全局模板中找到一个。如果同名的构建块在多个全局模板中,我相信使用的是全局第一个加载的,但我没有最后测试过。

标签: vba ms-word


【解决方案1】:

我回答了为什么段落标记出现在your question posted on Super User 中的部分。此响应是解决使用 vba 插入构建块的问题。但是,您的 vba 不会导致额外的段落标记。如超级用户的响应中所述,这是由于构建块的内容。

如果您的 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

情况3 - 模板持有积木是积木存储位置的“Building Blocks.dotx”

此模板也不包含宏,因为 Building blocks 文件夹中的模板不会向 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 编辑了这个答案。

【讨论】:

    猜你喜欢
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    • 2020-08-23
    • 1970-01-01
    • 2015-06-04
    • 2020-11-30
    • 2011-08-17
    相关资源
    最近更新 更多