【问题标题】:Running headers for multiple levels of headings多级标题的运行标题
【发布时间】:2017-08-09 14:41:38
【问题描述】:

我知道,有一种使用 STYLEREF 字段创建运行标头的简单方法。就这样吧:

{ STYLEREF "Heading 1" }

在文档的标题中,它工作正常。

但是,当我想匹配多个标题级别时,就会出现问题。例如,在我的文档的第一页上,我有一个带有文本 FooHeading 1 样式。在文档的第二页上,我有 Heading 2 样式和文本 Bar

当我在文档的第一页时,我想在页眉中看到“Foo”。当我在第 2 页时,我想在页眉中看到“栏”。

在 LibreOffice 中非常简单,但我在 MS Word 中没有找到任何“正确”的方法来实现它。

旁注:嗯,有一个解决方法:创建一个字符样式“我的标题”并将其应用于段落样式“标题 1”和“标题 2”,然后在 STYLEREF 中使用它字段:

{ STYLEREF "My headings" }

但是不方便。

我将其发布为 StackOverflow 问题,因为我相信,这可能可以通过宏来解决。

【问题讨论】:

  • 请您提供一个提示,它是如何在 LibreOffice 中完成的。它可以帮助创建宏。
  • @BrunoBieri 很容易。 步骤 1. 创建新文档 > 菜单 > 插入 > 页眉和页脚 > 页眉 > 默认样式。 第 2 步。 在那里,按 Ctrl-F2(或单击插入 > 字段 > 更多字段)> 在结果窗口中选择“文档”选项卡 > 选择章节,然后选择“章节名称”> 在下方键入“级别”输入字段中的“10”(它将匹配从级别 1 到级别 10 的标题)> 按“插入”。就这样。有时它的工作有点麻烦,但应该这样做。
  • 据我所知,使用 Microsoft Word 标准功能是不可能的。我已经尝试了几件事,但无法使其正常工作。我认为您需要编写一个 VBA 宏。
  • 谢谢,以后会注意的。
  • 我再次检查并尝试编写宏。宏是可能的,但困难开始将当前页眉样式文本插入页眉。由于页眉在每个页面上始终显示相同的内容,因此您需要在每个页面上引入“分节符”。并确保取消勾选“与以前的连接”。假设可以将每个页面的样式标题文本插入到每个页面的页眉中。这将是一个非常“hacky”的解决方案,您的文档将充满部分。我不想使用这样的文件。但这取决于你。

标签: vba ms-office ms-word


【解决方案1】:

我再次检查并尝试编写宏。宏是可能的,但是当将当前页眉样式文本插入页眉时,困难就开始了。由于在 Microsoft Word 中,页眉在每一页上总是显示相同的内容,您需要在每一页上引入Section break。然后,这将允许您在每个页面上拥有不同的页眉内容。此外,还必须确保未选中标题选项 connect with previous,这样才能正常工作。

假设可以将每个页面的样式标题文本插入到每个页面的页眉中。这将是一个非常“hacky”的解决方案,并且由于分节符,您的文档将充满部分。不过我不想使用这样的文档,但这取决于你。

这是我在意识到部分问题之前提出的 NOT WORKING 宏:

Sub RunningHeader()
' THIS MACRO DOES NOT WORK!

' Date: 2017.08.08
' Running header macro
' Assumes every page ends with a section break
' Supports to set running header up to level 3

Dim mPageCount As Integer
Dim mCurrentPage As Integer
Dim mPageRange As Range
Dim mSection As Section

Dim mRunningHeader As String

mPageCount = ActiveDocument.ComputeStatistics(wdStatisticPages)

' ToDo
' Ensure each page of the document ends with a "section break"

' Loop through each page.
' Idea looping through pages from:
' https://support.microsoft.com/en-us/help/269565/how-to-automate-word-to-set-and-retrieve-section-header-and-footer-inf
For mCurrentPage = 1 To mPageCount
    '
    If intpage <> 1 Then
        ' Goes to the top of the next page.
        Selection.GoToNext What:=wdGoToPage
    Else
        ' Goes to the top of the first page.
        Selection.HomeKey Unit:=wdStory
    End If

    ' Selects the content of the current page
    ActiveDocument.Bookmarks("\page").Range.Select

    ' Get text of highest header style on current page
    mRunningHeader = GetHighestHeader

    ' Get section of current page
    Set mPageRange = ActiveDocument.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=mCurrentPage)
    Set mSection = mPageRange.Sections(1)

    ' Disconnect page header from previous
    ' ToDo

    ' Set text into page header
    ' ToDo
Next

End Sub

Private Function GetHighestHeader() As String
    Dim mParagraph As Paragraph
    Dim mHighestHeaderText As String
    Dim mHighestHeaderNumber As Integer

    mHighestHeaderText = ""

    For Each mParagraph In Selection.Paragraphs
        If mParagraph.Style = ActiveDocument.Styles(wdStyleHeading1) Then
            mHighestHeaderText = mParagraph.Range.Text
            Exit For
        End If
        If mParagraph.Style = ActiveDocument.Styles(wdStyleHeading2) Then
            If mHighestHeaderNumber < 2 Then
                mHighestHeaderText = mParagraph.Range.Text
                mHighestHeaderNumber = 2
            End If
        End If
        If mParagraph.Style = ActiveDocument.Styles(wdStyleHeading3) Then
            If mHighestHeaderNumber < 3 Then
                mHighestHeaderText = mParagraph.Range.Text
                mHighestHeaderNumber = 3
            End If
        End If
    Next mParagraph

    GetHighestHeader = mHighestHeaderText
End Function

【讨论】:

    猜你喜欢
    • 2011-06-17
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 2019-03-28
    • 2013-07-20
    • 1970-01-01
    • 1970-01-01
    • 2012-07-24
    相关资源
    最近更新 更多