Incorrect page numbers after inserting files

J

JonSteng

MS XP / Word 2000 – 2003
Page Numbering Woes – I am having a hard time getting my document to
correctly number pages, any help greatly appreciated.

I am working on a macro that allows users to apply formatting to a document,
add table of contents and insert other documents (called boilerplates) to the
beginning of the original (after TOC).

I am able to insert the requested documents using the following code:
Note: at the time of inserting the documents the table of contents has
already been inserted.

Sub _Boilerplates()
'add selected boilerplates to current document
Dim iCnt As Integer
For iCnt = 0 To frmSpecMain.lbBoilerplates.ListCount - 1
If frmSpecMain.lbBoilerplates.Selected(iCnt) Then
Selection.HomeKey Unit:=wdStory
ActiveWindow.ActivePane.View.Type = wdPageView
' Go to second section
Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst, Count:=2, Name:=""
Selection.InsertFile FileName:=gstrDocFullPath & _
frmSpecMain.lbBoilerplates.List(iCnt), _
Range:="", ConfirmConversions:=False, Link:=False, Attachment:=False
Selection.InsertBreak Type:=wdSectionBreakContinuous
End If
Next
ActiveDocument.Save
End Sub

After inserting the boilerplates I run the following code to number the pages:

Sub _PageNumbers()
'
' Add page numbers to the final document
'
Dim i As Integer
i = Selection.Sections.Count

' go to top of document and clear find area
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting

Selection.HomeKey Unit:=wdStory

With Selection.Sections(1).Headers(1).PageNumbers
.NumberStyle = wdPageNumberStyleLowercaseRoman
.HeadingLevelForChapter = 0
.IncludeChapterNumber = False
.ChapterPageSeparator = wdSeparatorHyphen
.RestartNumberingAtSection = True
.StartingNumber = 1
End With

Selection.Sections(1).Footers(1).PageNumbers.Add PageNumberAlignment:= _
wdAlignPageNumberRight, FirstPage:=True

For i = 1 To Selection.Sections.Count + 1

' Skip to Next Section and number those pages
Selection.GoTo What:=wdGoToSection, Which:=wdGoToNext, Count:=1, Name:=""
Selection.Sections(1).Footers(1).LinkToPrevious = False

' Now number body
With Selection.Sections(1).Headers(1).PageNumbers
.NumberStyle = wdPageNumberStyleArabic
.HeadingLevelForChapter = 0
.IncludeChapterNumber = False
.ChapterPageSeparator = wdSeparatorHyphen
.RestartNumberingAtSection = True
.StartingNumber = i - 1
End With
Selection.Sections(1).Footers(1).PageNumbers.Add PageNumberAlignment:= _
wdAlignPageNumberRight, FirstPage:=True
Next


End Sub


What happens is that the newly inserted boilerplates all have the page
number 1, as well as the beginning of the original document.

Harrumph.

Again any suggestions greatly appreciated, thank you all from Sunny Central
Florida.
 
J

JonSteng

I believe I answered my own question, sorry for the bother. For those
interested here is what I did:

I made two changes... first in the _boilerplates routine that inserts files
I updated the following line:
Selection.InsertBreak Type:=wdSectionBreakContinuous

to this
Selection.InsertBreak Type:=wdPageBreak

Next I update the page numbering routine - boy was I over complicating that
process! Since I was inserting Section breaks after each file I assumed that
there would be multiple sections, but when I tried to loop through them, it
failed.

After changing from section break to page break I removed the loop attempt
and simply went to the section after the Table Of Contents and numbered the
pages.

KISS, rookie mistkes and hours lost.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top