PDA

View Full Version : [SOLVED:] Macro to make page numbering to start at 1 from each new section (Word 2010)



Eperluette
02-13-2015, 09:24 AM
Hello everyone,

I was wondering if there is a simple way to solve the following problem.

I have a Word document that contains more than 100 different sections (Next Page Section Break). I would like the page numbering to start from 1 at the beginning of each new sections (in the footer).
One way to do this is to go to Page Number Format and select 'Start at 1', then move on to the next footer and repeat.

Is there a way to do this for all sections at once without having to edit each section individually? I have tried to create a macro with the macro recorder but it doesn't work :/

This is my code so far:


Sub Section_Numbers()
'
' Section_Numbers Macro
'
Dim curSection As Section, curHeader As HeaderFooter
For Each curSection In ActiveDocument.Sections


For Each curFooter In curSection.Footers


With Selection.HeaderFooter.PageNumbers
.NumberStyle = wdPageNumberStyleArabic
.HeadingLevelForChapter = 0
.IncludeChapterNumber = False
.ChapterPageSeparator = wdSeparatorHyphen
.RestartNumberingAtSection = True
.StartingNumber = 1
End With


Next curFooter
Next curSection


End Sub

I would appreciate any help or advice with this :)

gmaxey
02-13-2015, 05:34 PM
Perhaps:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oSec As Section
Dim i As Long
For Each oSec In ActiveDocument.Sections
oSec.Footers(wdHeaderFooterPrimary).PageNumbers.StartingNumber = 1
Next oSec
End Sub

Eperluette
02-16-2015, 03:32 AM
Hi Greg,

Thank you so much for your reply. I just tried your macro but it doesn't seem to work. I have tested it with/without the footers linked to previous to see if it would make a difference but on both instances the footers stayed blank. Perhaps I am missing something?

I am attaching a test page I am working on if that helps: 12856

Thank you very much for your help!

Sarah

gmayor
02-16-2015, 03:48 AM
It doesn't work because there are no page numbers in the document. The following modification will add the numbers at the centre of your footers and will restart the numbering at each section.

IMPORTANT - the macro assumes there is no required text in the footer, as it will clear the footer completely before adding the numbers. If there is wanted text in the footer you will need to explain how the numbering is positioned relative to that text



Dim oSec As Section
Dim oFooter As HeaderFooter
For Each oSec In ActiveDocument.Sections
For Each oFooter In oSec.Footers
oFooter.LinkToPrevious = False
If oFooter.Exists Then
oFooter.Range.Text = ""
oFooter.Range.Fields.Add oFooter.Range, wdFieldPage, , False
oFooter.PageNumbers.RestartNumberingAtSection = True
oFooter.PageNumbers.StartingNumber = 1
oFooter.Range.InsertBefore vbTab & "Page "
End If
Next oFooter
Next oSec

Eperluette
02-16-2015, 05:53 AM
Hi Graham,

This is brilliant and exactly what I wanted.
Thank you so much for your help!

Cheers,

Sarah

dg406
09-14-2016, 06:12 AM
It doesn't work because there are no page numbers in the document. The following modification will add the numbers at the centre of your footers and will restart the numbering at each section.

IMPORTANT - the macro assumes there is no required text in the footer, as it will clear the footer completely before adding the numbers. If there is wanted text in the footer you will need to explain how the numbering is positioned relative to that text



Dim oSec As Section
Dim oFooter As HeaderFooter
For Each oSec In ActiveDocument.Sections
For Each oFooter In oSec.Footers
oFooter.LinkToPrevious = False
If oFooter.Exists Then
oFooter.Range.Text = ""
oFooter.Range.Fields.Add oFooter.Range, wdFieldPage, , False
oFooter.PageNumbers.RestartNumberingAtSection = True
oFooter.PageNumbers.StartingNumber = 1
oFooter.Range.InsertBefore vbTab & "Page "
End If
Next oFooter
Next oSec

Hello gmayor,

I was wondering if you would be kind enough to help me adapt this code so that I can do something similar to your quoted answer for 7 documents I need to collate for my thesis?

I found a possible solution to my problem online but it doesn't seem to work for me (I've tried changing the code to "Footers" with no success). The idea is to open each document in turn (which happens using the code below) and to access the footer similarly to the example code you have supplied - although I want to add the page numbers in a particular format. I also have content in the headers which I wish to keep.

The current format of the footer (which i would like to keep if possible) is an inserted table with two rows and three columns. The page number is right aligned in the bottom-right cell of the table. I'm not sure if this makes any difference to the approach, but there is a thick border line (1.5pt) running between the two rows of the table.

I hope I have explained myself clearly and that you would be kind enough to help with what I'm sure is a relatively simple problem for an experienced VBA user.

Thanks and best wishes

Dan



Sub PageNumberReset()
Dim pgNo As Long
Dim n As Long
Dim pathName As String
Dim fileNames
Dim thisFile As String
Dim aRange As Range

' Specify the path to the document files
pathName = "C:\MyDocs\Example\"
' Create an array holding the document file names, in sequence
fileNames = Array("Chap1.docx", "Chap2.docx", "Chap3.docx")

pgNo = 0
For n = 0 To UBound(fileNames)
thisFile = pathName & fileNames(n)
Application.Documents.Open (thisFile)
ActiveDocument.Sections(1).Headers(1).PageNumbers.StartingNumber = pgNo + 1
Set aRange = ActiveDocument.Range
aRange.Collapse Direction:=wdCollapseEnd
aRange.Select
pgNo = Selection.Information(wdActiveEndAdjustedPageNumber)
Application.Documents(thisFile).Close Savechanges:=wdSaveChanges
Next n
End Sub