运行标题的多个级别的标题 [英] Running headers for multiple levels of headings

查看:110
本文介绍了运行标题的多个级别的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,有一种使用STYLEREF字段创建运行标头的简单方法.只需输入以下内容即可:

I know, there is a simple way to create running headers using STYLEREF field. Just put this:

{ STYLEREF "Heading 1" }

在文档标题中,效果很好.

in the header of your document, and it works fine.

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

However, the problem arises when I want to match multiple heading levels. For example, on 1st page of my document I have a Heading 1 style with text Foo. And on the 2nd page of document I have Heading 2 style with text Bar.

当我在文档的第一页上时,我想在页面标题中看到"Foo".当我进入第二页时,我想在页面标题中看到栏".

When I'm on 1st page of document, I want to see the "Foo" in the page header. When I'm on the 2nd page, I want to see "Bar" in the page header.

在LibreOffice中这非常简单,但是我还没有找到在MS Word中实现它的任何适当"方法.

It is very simple in LibreOffice, but I haven't find any "proper" way to achieve it in MS Word.

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

Sidenote: Well, there exists a workaround: create a character style "My headings" and apply it on the paragraph styles "Heading 1" and "Heading 2", and then use it in STYLEREF field:

{ STYLEREF "My headings" }

但这不方便.

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

I post it as StackOverflow question, because I believe, this probably could be fixed with macro.

推荐答案

我再次检查并尝试编写宏.可以使用该宏,但是在将当前页眉样式文本插入页面页眉中时,困难就开始了.由于在Microsoft Word中,页眉在每个页面上始终显示相同的内容,因此您需要在每个页面上引入Section break.然后,这将使您在每个页面上具有不同的页面标题内容.另外,还必须确保取消选中标题选项connect with previous,这样才能完全起作用.

I checked again and tried to write a macro. The macro would be possible but the difficulties starts when it comes to insert the current header style text into the page header. Since in Microsoft Word the page headers display always the same content on every page, you would need to introduce Section break on every single page. This would then allow you to have different page header content on every page. Also it's necessary to ensure that the header option connect with previous is unticked so this would work at all.

鉴于可以将每个页面的样式标题文本插入每个页面的页面标题中.这将是一个真正的"hacky"解决方案,由于分节符,您的文档将充满节.尽管我不想使用这样的文档,但这取决于您.

Given that it would be possible to insert the style header text of each page into the page headers of each page. It would be a really "hacky" solution and your document would be full of sections due to the section breaks. I wouldn't want to work with such a document though but that's up to you.

这是我想出的不工作宏,直到我意识到本节的问题:

Here is the NOT WORKING macro I've came up with until I realized the section issue:

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

这篇关于运行标题的多个级别的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆