Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-20-2020, 07:01 AM
mbr50 mbr50 is offline Page Numbering Macro Mac OS X Page Numbering Macro Office 2016 for Mac
Novice
Page Numbering Macro
 
Join Date: Jan 2020
Posts: 3
mbr50 is on a distinguished road
Default Page Numbering Macro

I have created a macro to number the pages of a book according to publishing convention:



- The preliminaries (section 1 of the file) is numbered by Roman numerals (i, ii, iii...x)
- Sections 2 to x are numbered by Arabic numerals (1,2,3...500)
- The first page of each section is unnumbered

The file is divided into 15 sections, and different rules applied to the footers for section 1, section 2 (which switches from Roman to Arabic numbers and has the first page unnumbered), and the remaining sections (where each first page must be unnumbered).

The trouble is that this code produces two {PAGE} fields in the footer to each section. Where is this field being created and how do I stop it?

Code:
Sub CreatePageRefs()

    Application.ScreenUpdating = False
    Dim rng As Word.Range, Sec As Word.Section, ft As Word.HeaderFooter
    
    Set Sec = ActiveDocument.Sections(1) 'The first section, in Roman (i, ii, iii)
    Set ft = Sec.Footers(wdHeaderFooterPrimary)
    Set rng = ft.Range
    With ft.PageNumbers
        .NumberStyle = wdPageNumberStyleLowercaseRoman
        .RestartNumberingAtSection = True
        .StartingNumber = 1
        .ShowFirstPageNumber = False
    End With
    rng.Style = wdStyleFooter
    rng.ParagraphFormat.Alignment = wdAlignParagraphCenter
    rng.Collapse wdCollapseEnd
    rng.Fields.Add Range:=rng, Type:=wdFieldPage
    
    Set Sec = ActiveDocument.Sections(2) 'The second section, 
                                                             'switch to Arabic numbering (1,2,3)
    Set ft = Sec.Footers(wdHeaderFooterPrimary)
    Set rng = ft.Range
    ft.LinkToPrevious = False
    With ft.PageNumbers
        .NumberStyle = wdPageNumberStyleArabic
        .RestartNumberingAtSection = True
        .StartingNumber = 1
        .ShowFirstPageNumber = False
    End With
    
    Dim numOfSections As Integer
    numOfSections = ActiveDocument.Sections.Count
    
    For i = 3 To numOfSections 'All remaining sections 
                                             ' -- restart so that first page number set to "False"
    Set Sec = ActiveDocument.Sections(i)
        Set ft = Sec.Footers(wdHeaderFooterPrimary)
       ft.LinkToPrevious = False
       Set rng = ft.Range
        With ft.PageNumbers
            .NumberStyle = wdPageNumberStyleArabic
            .RestartNumberingAtSection = False
            .StartingNumber = 1
            .ShowFirstPageNumber = False
        End With
    Next

End Sub
This code produces two center aligned fields {PAGE} and {PAGE \* MERGEFORMAT } in section 1. In the remaining sections, {PAGE} is right aligned, and {PAGE \* MERGEFORMAT } is centered. In all cases, I want just one, center-aligned {PAGE} field.
Reply With Quote
  #2  
Old 01-20-2020, 01:29 PM
gmaxey gmaxey is offline Page Numbering Macro Windows 10 Page Numbering Macro Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Something like this:
Code:
Sub CreatePageRefs()

    Application.ScreenUpdating = False
    Dim rng As Word.Range, Sec As Word.Section, ft As Word.HeaderFooter, i As Long
    
    Set Sec = ActiveDocument.Sections(1) 'The first section, in Roman (i, ii, iii)
    Set ft = Sec.Footers(wdHeaderFooterPrimary)
    Set rng = ft.Range
    With ft.PageNumbers
        .NumberStyle = wdPageNumberStyleLowercaseRoman
        .RestartNumberingAtSection = True
        .StartingNumber = 1
        .ShowFirstPageNumber = False
    End With
    'rng.Style = wdStyleFooter
    rng.ParagraphFormat.Alignment = wdAlignParagraphCenter
    rng.Collapse wdCollapseEnd
    'rng.Fields.Add Range:=rng, Type:=wdFieldPage
    
    Set Sec = ActiveDocument.Sections(2) 'The second section,
                                                             'switch to Arabic numbering (1,2,3)
    Set ft = Sec.Footers(wdHeaderFooterPrimary)
    Set rng = ft.Range
    ft.LinkToPrevious = False
    With ft.PageNumbers
        .NumberStyle = wdPageNumberStyleArabic
        .RestartNumberingAtSection = True
        .StartingNumber = 1
        .ShowFirstPageNumber = False
    End With
    
    Dim numOfSections As Integer
    numOfSections = ActiveDocument.Sections.Count
    
    For i = 3 To numOfSections 'All remaining sections
                                             ' -- restart so that first page number set to "False"
    Set Sec = ActiveDocument.Sections(i)
        Set ft = Sec.Footers(wdHeaderFooterPrimary)
       ft.LinkToPrevious = False
       Set rng = ft.Range
        With ft.PageNumbers
            .NumberStyle = wdPageNumberStyleArabic
            .RestartNumberingAtSection = True
            .StartingNumber = 1
            .ShowFirstPageNumber = False
        End With
    Next

End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #3  
Old 01-20-2020, 02:15 PM
mbr50 mbr50 is offline Page Numbering Macro Mac OS X Page Numbering Macro Office 2016 for Mac
Novice
Page Numbering Macro
 
Join Date: Jan 2020
Posts: 3
mbr50 is on a distinguished road
Default

I have substituted your
Code:
.RestartNumberingAtSection = True
in the third loop and this works almost perfectly. Thank you.

I am still unable to work out how to center the page number on the page (it currently sits in a frame on its own aligned to the right of the document), because
Code:
rng.ParagraphFormat.Alignment = wdAlignParagraphCenter
centers the paragraph, but not the page number.

How do I center the page number frame in VBA? This is quite easy to do by right clicking, Format Frame > Position > Center, but I cannot replicate this programmatically.
Reply With Quote
  #4  
Old 01-20-2020, 03:59 PM
gmaxey gmaxey is offline Page Numbering Macro Windows 10 Page Numbering Macro Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,421
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Code:
Sub CreatePageRefs()
  Dim oSec As Word.Section, oFooter As Word.HeaderFooter, lngIndex As Long
  Set oSec = ActiveDocument.Sections(1) 'The first section, in Roman (i, ii, iii)
  oSec.PageSetup.DifferentFirstPageHeaderFooter = True
  Set oFooter = oSec.Footers(wdHeaderFooterPrimary)
  With oFooter.PageNumbers
    .Add PageNumberAlignment:=wdAlignPageNumberCenter
    .NumberStyle = wdPageNumberStyleLowercaseRoman
    .RestartNumberingAtSection = True
    .StartingNumber = 1
    .ShowFirstPageNumber = False
  End With
  Set oSec = ActiveDocument.Sections(2) 'The second section switch to Arabic numbering (1,2,3)
  oSec.PageSetup.DifferentFirstPageHeaderFooter = True
  Set oFooter = oSec.Footers(wdHeaderFooterPrimary)
  oFooter.LinkToPrevious = False
  With oFooter.PageNumbers
    .Add PageNumberAlignment:=wdAlignPageNumberCenter
    .NumberStyle = wdPageNumberStyleArabic
    .RestartNumberingAtSection = True
    .StartingNumber = 1
    .ShowFirstPageNumber = False
  End With
  For lngIndex = 3 To ActiveDocument.Sections.Count 'All remaining sections restart so that first page number set to "False"
    Set oSec = ActiveDocument.Sections(lngIndex)
    oSec.PageSetup.DifferentFirstPageHeaderFooter = True
    Set oFooter = oSec.Footers(wdHeaderFooterPrimary)
    oFooter.LinkToPrevious = False
    With oFooter.PageNumbers
      .Add PageNumberAlignment:=wdAlignPageNumberCenter
      .NumberStyle = wdPageNumberStyleArabic
      .RestartNumberingAtSection = True
      .StartingNumber = 1
      .ShowFirstPageNumber = False
    End With
  Next
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote
  #5  
Old 01-20-2020, 04:47 PM
mbr50 mbr50 is offline Page Numbering Macro Mac OS X Page Numbering Macro Office 2016 for Mac
Novice
Page Numbering Macro
 
Join Date: Jan 2020
Posts: 3
mbr50 is on a distinguished road
Default

This works perfectly. Thanks!
Reply With Quote
Reply

Tags
field codes, headers and footers, page numbers

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Page Numbering Macro Page-numbering question: continue numbering from one section to the next? {PAGE} of {SECTIONPAGES} SDwriter Word 12 10-25-2017 06:56 AM
Page Numbering Macro How to add page numbering after next page break with landscape and portrait Jashley1 Word 2 11-25-2014 10:36 PM
Page Numbering Macro begin each section with page 1 in header PLUS continuous page numbering in footer onemorecupofcoffee Word 18 09-04-2013 04:31 PM
Page Numbering Macro Image page numbering macro panel PowerPoint 1 08-16-2012 06:33 AM
Page Header and Page Numbering for Technical Book SQLUSA Word 4 06-25-2012 09:53 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:43 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft