运行 headers 多级标题

Running headers for multiple levels of headings

我知道,有一种使用 STYLEREF 字段创建 运行 header 的简单方法。只需输入:

{ STYLEREF "Heading 1" }

在您文档的 header 中,它工作正常。

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

当我在文档的第一页时,我想在第 header 页中看到 "Foo"。当我在第 2 页时,我想在 header.

页中看到 "Bar"

在 LibreOffice 中非常简单,但我还没有找到任何 "proper" 在 MS Word 中实现它的方法。

旁注: 嗯,有一个解决方法:创建一个字符样式 "My headings" 并将其应用于段落样式 "Heading 1" 和 "Heading 2",然后在 STYLEREF 字段中使用它:

{ STYLEREF "My headings" }

但是不方便

我 post 它是 Whosebug 的问题,因为我相信,这可能可以用宏来解决。

我又查了一下,试着写了一个宏。宏是可行的,但是当涉及到将当前 header 样式文本插入页面 header 时,困难就开始了。由于在 Microsoft Word 中,页面 header 始终在每一页上显示相同的内容,因此您需要在 每一页 上引入 Section break。这将允许您在每个页面上有不同的页面 header 内容。此外,有必要确保 header 选项 connect with previous 未被勾选,这样才能正常工作。

鉴于可以将每个页面的样式 header 文本插入到每个页面的页面 header 中。这将是一个真正的 "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