VBA/Excel/Access/Word/Word/Document Header Footer
Содержание
- 1 Adding Page Numbers to One or More Sections of a Document
- 2 adds a new-page section to the active document, placing it before the second paragraph:
- 3 Changing the Page Numbering for a Section
- 4 Creating "Page X of Y" Page Numbers
- 5 Finding Out if a Section of a Document Has Page Numbers
- 6 Formatting Page Numbers
- 7 Getting to the Header or Footer
- 8 Removing Page Numbers from One or More Sections of a Document
- 9 Select the PageNumber object and then apply formatting to it as you would any other selection
- 10 Suppressing the Page Number for the First Page: set the ShowFirstPageNumber property for the appropriate HeaderFooter object in the appropriate section to False
- 11 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
Adding Page Numbers to One or More Sections of a Document
<source lang="vb">
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
</source>
adds a new-page section to the active document, placing it before the second paragraph:
<source lang="vb">
Sub break()
ActiveDocument.Sections.add Range:=ActiveDocument.Range(Start:=ActiveDocument.Paragraphs(2).Range.Start, _ End:=ActiveDocument.Paragraphs(2).Range.End), Start:=wdSectionNewPage
End Sub
</source>
Changing the Page Numbering for a Section
<source lang="vb">
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
</source>
Creating "Page X of Y" Page Numbers
<source lang="vb">
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
</source>
Finding Out if a Section of a Document Has Page Numbers
<source lang="vb">
Sub find() If Selection.Sections(1).Headers(wdHeaderFooterEvenPages) _
.PageNumbers.Count = 0 Then Selection.Sections(1) _ .Headers(wdHeaderFooterEvenPages).PageNumbers.add _ PageNumberAlignment:=wdAlignPageNumberCenter
End Sub
</source>
Formatting Page Numbers
<source lang="vb">
Sub numStyle()
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) _ .PageNumbers.NumberStyle = wdPageNumberStyleLowercaseLetter
End Sub
</source>
<source lang="vb">
Sub header()
MsgBox Documents("Transfer.doc").Sections(2). _ Footers(wdHeaderFooterFirstPage).Range.Text
End Sub
</source>
Removing Page Numbers from One or More Sections of a Document
<source lang="vb">
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
</source>
Select the PageNumber object and then apply formatting to it as you would any other selection
<source lang="vb">
Sub pageNumber()
ActiveDocument.Sections(4).Headers(wdHeaderFooterPrimary) _ .PageNumbers(1).Select With Selection.Font .Name = "Impact" .Size = 20 .Bold = True End With
End Sub
</source>
<source lang="vb">
Sub page()
ActiveDocument.Sections(1).Footers(wdHeaderFooterPrimary).PageNumbers_ .ShowFirstPageNumber = False
End Sub
</source>
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
<source lang="vb">
Sub add()
With ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary) _ .PageNumbers .IncludeChapterNumber = True .ChapterPageSeparator = wdSeparatorEnDash End With
End Sub
</source>