![]() |
|
#1
|
|||
|
|||
|
Hi
Would there be a VBA Code for inserting page numbers to word document. The VBA code would insert a page number from a selected starting page. The text would be Page 1 and then number every page after the selected starting page. This would be inserted at the bottom , left hand side of each page Thanks you Rich |
|
#2
|
||||
|
||||
|
You don't really need a macro to do this, but the following will do it. There are however provisos and the main one revolves around how the footers are configured. There are potentially lots of footer ranges in a document - three to each section. This macro addresses only the primary footer.
Code:
Option Explicit
Sub AddPageNumbers()
Dim oRng As Range
Dim oFooter As HeaderFooter
Set oRng = ActiveDocument.Bookmarks("\page").Range
oRng.Collapse 1
oRng.InsertBreak wdSectionBreakContinuous
Set oFooter = oRng.Sections(1).Footers(wdHeaderFooterPrimary)
oFooter.LinkToPrevious = False
If Len(oFooter.Range.Text) > 1 Then
oFooter.Range.InsertParagraphAfter
End If
With oFooter.PageNumbers
.NumberStyle = wdPageNumberStyleArabic
.HeadingLevelForChapter = 0
.IncludeChapterNumber = False
.ChapterPageSeparator = wdSeparatorHyphen
.RestartNumberingAtSection = True
.StartingNumber = 1
End With
Set oRng = oFooter.Range
With oRng
.Collapse 0
.Text = "Page "
.Collapse 0
.Fields.Add oRng, wdFieldPage, , False
.ParagraphFormat.Alignment = wdAlignParagraphLeft
End With
lbl_Exit:
Set oFooter = Nothing
Set oRng = Nothing
Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How do I edit my TOC to reference the section numbers instead of the page numbers??? | mikey386 | Word | 0 | 12-17-2014 02:34 PM |
How to enter different texts on the bottom of the page?
|
Pws | Word | 5 | 02-24-2014 11:54 PM |
Page breaks....Ctrl-Enter..help is appreciated
|
typing33 | Word | 2 | 02-10-2013 02:19 PM |
| How do I refer to page numbers, when the numbers change as I prepare the document? | StevenD | Word | 5 | 11-29-2012 12:52 AM |
Page Numbers Not Matching Chapter Numbers
|
gracie5290 | Word | 1 | 02-02-2012 11:41 PM |