将页眉和页脚添加到多页Word文档VBA [英] Add Header and Footer to mulitpage word doc VBA

查看:435
本文介绍了将页眉和页脚添加到多页Word文档VBA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过宏向Word文档的每一页添加页眉和页脚.

I am trying to add a header and a footer to each page of a word document via a macro.

我尝试了几种不同的方法,例如遍历页面上的每种形状,但是在这种情况下,页眉和页脚会在每页上多次打印出来,具体取决于文档中有多少种形状.

I have tried a few different methods such as iterating through each shape on the page but in that case , the header and footer prints out multiple times on each page depending on how many shapes are in the document.

当前,我的代码正在查找任何当前的页眉和页脚并将其删除,然后仅将我的页眉和页脚插入第一页,并将其余页面保留在文档的页眉和页脚中.

Currently my code is looking for any current header and footer and deleting them, then it just inserts my header and footer on the first page and leaves the remaining pages in the document's header and footer blank.

谁能告诉我我要去哪里错了?

Can anyone tell me where I am going wrong?

Sub HeaderFooter()
    Dim oSec As Section
    Dim oHead As HeaderFooter
    Dim oFoot As HeaderFooter

    For Each oSec In ActiveDocument.Sections
        For Each oHead In oSec.Headers
            If oHead.Exists Then oHead.Range.Delete
        Next oHead

        For Each oFoot In oSec.Footers
            If oFoot.Exists Then oFoot.Range.Delete
        Next oFoot
    Next oSec

    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
    With Selection.PageSetup
        .HeaderDistance = CentimetersToPoints(1.0)
        .FooterDistance = CentimetersToPoints(1.0)
    End With
    Selection.InlineShapes.AddPicture FileName:="image.jpg" _
        , LinkToFile:=False, SaveWithDocument:=True
    Selection.ParagraphFormat.Alignment = wdAlignParagraphRight

    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
    Selection.Font.Color = RGB(179, 131, 89)
    Selection.Font.Size = 10
    Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
    Selection.TypeText Text:="footer test"      
End Sub

推荐答案

您需要根据页眉/页脚将页眉/页脚添加到第一页的wdHeaderFooterFirstPage范围内,并将所有其他页的页眉/页脚添加到wdHeaderFooterPrimary文档的设置.

You need to add the header/footer into the wdHeaderFooterFirstPage range for the first page and into wdHeaderFooterPrimary for all other pages depending on the header/footer settings of the document.

下面的示例在所有页面中创建一个标题,该标题由带有两个单元格的表组成.左侧为图片,右侧为文字.

The example below creates a header in all pages, consisting of a table with two cells. An image on the left side and text on the right side.

Sub UpdateHeader()

    Dim oDoc As Word.Document, oSec As Word.Section, rng As Word.Range
    Set oDoc = ActiveDocument

    For Each oSec In oDoc.Sections
        Set rng = oSec.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage).Range
        AddHeaderToRange rng

        Set rng = oSec.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
        AddHeaderToRange rng
    Next oSec
End Sub


Private Sub AddHeaderToRange(rng As Word.Range)
    With rng
        .Tables.Add Range:=rng, NumRows:=1, NumColumns:=2, DefaultTableBehavior:=wdWord8TableBehavior, AutoFitBehavior:=wdAutoFitFixed
        With .Tables(1)
            .Borders.InsideLineStyle = wdLineStyleNone
            .Borders.OutsideLineStyle = wdLineStyleNone
            .Rows.SetLeftIndent LeftIndent:=-37, RulerStyle:=wdAdjustNone
            .Columns(2).SetWidth ColumnWidth:=300, RulerStyle:=wdAdjustNone
            .Cell(1, 1).Range.InlineShapes.AddPicture filename:="image path", LinkToFile:=False, SaveWithDocument:=True
            .Cell(1, 2).Range.Font.Name = "Arial"
            .Cell(1, 2).Range.Font.Size = 9
            .Cell(1, 2).Range.ParagraphFormat.Alignment = wdAlignParagraphRight
            .Cell(1, 2).Range.Text = "Test header"
        End With
    End With
End Sub


相同的原则适用于页脚.

The same principle applies for the Footer.

Sub UpdateFooter()

    Dim oDoc As Word.Document, oSec As Word.Section, rng As Word.Range
    Set oDoc = ActiveDocument

    For Each oSec In oDoc.Sections
        Set rng = oSec.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage).Range
        AddFooterToRange rng

        Set rng = oSec.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
        AddFooterToRange rng

        Set rng = oSec.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterEvenPages).Range
        AddFooterToRange rng
    Next oSec
End Sub

Private Sub AddFooterToRange(rng As Word.Range)
    With rng
        .Font.Name = "Arial"
        .Font.Size = 9
        .Text = "Footer sample text"
        With .ParagraphFormat
            .Alignment = wdAlignParagraphJustify
            .LineSpacingRule = wdLineSpaceExactly
            .LineSpacing = Application.LinesToPoints(1)
            .LeftIndent = Application.CentimetersToPoints(-1.6)
            .RightIndent = Application.CentimetersToPoints(-1.6)
        End With
    End With
End Sub


最后,要删除现有的标头:

Lastly, to delete existing headers:

Sub ClearExistingHeaders(oDoc As Word.Document)
    Dim oSec As Word.Section, oHeader As HeaderFooter
    For Each oSec In oDoc.Sections
        For Each oHeader In oSec.Headers
            oHeader.Range.Delete
        Next
    Next
End Sub

这篇关于将页眉和页脚添加到多页Word文档VBA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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