VBA/Excel/Access/Word/Word/Document Header Footer

Материал из VB Эксперт
Перейти к: навигация, поиск

Adding Page Numbers to One or More Sections of a Document

 
Sub AddPageNumbersToAllHeadersAndSections()
    Dim cHeader As HeaderFooter, cSection As Section
    With Documents("YourDoc.doc")
        For Each cSection In .Sections
            For Each cHeader In cSection.Headers
                cSection.Headers(wdHeaderFooterPrimary).PageNumbers _
                    .Add.PageNumberAlignment:= _
                    wdAlignPageNumberRight, FirstPage:=True
            Next cHeader
        Next cSection
    End With
End Sub



adds a new-page section to the active document, placing it before the second paragraph:

 
Sub break()
    ActiveDocument.Sections.add Range:=ActiveDocument.Range(Start:=ActiveDocument.Paragraphs(2).Range.Start, _
            End:=ActiveDocument.Paragraphs(2).Range.End), Start:=wdSectionNewPage
End Sub



Changing the Page Numbering for a Section

 
Sub active()
    With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary)
        If .PageNumbers.StartingNumber = 0 Then
            .PageNumbers.RestartNumberingAtSection = True
            .PageNumbers.StartingNumber = 50
        End If
    End With
End Sub



Creating "Page X of Y" Page Numbers

 
Sub pageNumber()
    ActiveDocument.Sections(ActiveDocument.Sections.Count) _
        .Headers(wdHeaderFooterPrimary).Range.Select
    With Selection
        .Paragraphs(1).Alignment = wdAlignParagraphCenter
        .TypeText Text:="Page "
        .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
            "PAGE ", PreserveFormatting:=True
        .TypeText Text:=" of "
        .Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
            "NUMPAGES ", PreserveFormatting:=True
    End With
End Sub



Finding Out if a Section of a Document Has Page Numbers

 
Sub find()
If Selection.Sections(1).Headers(wdHeaderFooterEvenPages) _
    .PageNumbers.Count = 0 Then Selection.Sections(1) _
    .Headers(wdHeaderFooterEvenPages).PageNumbers.add _
    PageNumberAlignment:=wdAlignPageNumberCenter
End Sub



Formatting Page Numbers

 
Sub numStyle()
    ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) _
        .PageNumbers.NumberStyle = wdPageNumberStyleLowercaseLetter
End Sub



Getting to the Header or Footer

 
Sub header()
    MsgBox Documents("Transfer.doc").Sections(2). _
        Footers(wdHeaderFooterFirstPage).Range.Text
End Sub



Removing Page Numbers from One or More Sections of a Document

 
Sub RemovePageNumbersFromCurrentSection()
    Dim ThisHeader As HeaderFooter
    Dim ThisPageNumber As PageNumber
    With Selection.Sections(1)
        For Each ThisHeader In .Headers
            For Each ThisPageNumber In ThisHeader.PageNumbers
                ThisPageNumber.Delete
            Next ThisPageNumber
        Next ThisHeader
    End With
End Sub



Select the PageNumber object and then apply formatting to it as you would any other selection

 
Sub pageNumber()
    ActiveDocument.Sections(4).Headers(wdHeaderFooterPrimary) _
        .PageNumbers(1).Select
        With Selection.Font
            .Name = "Impact"
            .Size = 20
            .Bold = True
        End With
End Sub



Suppressing the Page Number for the First Page: set the ShowFirstPageNumber property for the appropriate HeaderFooter object in the appropriate section to False

 
Sub page()
    ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).PageNumbers_
        .ShowFirstPageNumber = False
End Sub



To add the chapter number to the page numbers, use heading numbering in your document, set the IncludeChapterNumber property to True, and specify the separator to use

 
Sub add()
    With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) _
        .PageNumbers
        .IncludeChapterNumber = True
        .ChapterPageSeparator = wdSeparatorEnDash
    End With
End Sub