0

We have large documents that use legal numbering (1, 1.1, 1.1.1) for main content and alphabetic letters for appendices (e.g. Appendix A, B, C, etc.), and we re-start numbering for each appendix. We set up a separate list style for the appendices with lettered numbering, so that much is fine. The problem is that when page numbers appear in the Table of Contents and Figures, people looking at printed copies don’t which page 1 to go to, so we tried adding ‘chapter’ numbers before the page numbers. That was the only way we found to display them in the TofC. But the chapter numbers Word adds are always formatted as Arabic numerals (1-1, 2-1 instead of A-1, B-1). (The Insert Page Number dialog in Word lets you format the page number, but not the chapter number for some reason.) So I’m hoping there is a way to do that using VBA.

The documentation for the PageNumbers object seems to indicate it can be done, since it says you have to apply a list template before you can include chapter numbers. (Wouldn’t Word use the numbering scheme from the template?), but I haven’t been able to figure out how to do it.

I tried running the sample code (See Sub SetChapterPageNumbers below), and it didn’t work. (It displayed the chapter number + 1, not the chapter number (‘Chapter 1’, pg. 1 displayed as ‘2-1’), and since the list style used Arabic numbers for the chapter, it didn’t tell me whether the list numbering format was used. Apart from that, it appeared to set a ListFormat for the entire document and we just want to apply it to the appendices. When I tried to apply a ListFormat for just the first appendix, using the sample as a model, all it did was set all the content in that section to the Appendix Heading Level 1 style:

Set wdDoc = ActiveDocument
wdDoc.Sections(5).Range.ListFormat.ApplyListTemplate ListTemplate:=wdDoc.Styles("Appendices").ListTemplate

Sub SetChapterPageNumbers()
'copied from Microsoft PageNumbers.HeadingLevelFormat example
'The first part of this example creates a new document, adds chapter titles and page breaks, and then formats the document by using the last numbered outline format listed in the Bullets and Numbering dialog box.
'The second part of the example adds centered page numbers - including the chapter number - to the header; an en dash separates the chapter number and the page number. The first heading level is used for the chapter number, and lowercase roman numerals are used for the page number.

Dim intLoop As Integer
Dim hdrTemp As HeaderFooter
 
Documents.Add
For intLoop = 1 To 5
 With Selection
 .TypeParagraph
 .InsertBreak
 End With
Next intLoop
ActiveDocument.Content.Style = wdStyleHeading1
ActiveDocument.Content.ListFormat.ApplyListTemplate _
 ListTemplate:=ListGalleries(wdOutlineNumberGallery) _
 .ListTemplates(7)
 
Set hdrTemp = ActiveDocument.Sections(1) _
 .Headers(wdHeaderFooterPrimary)
 
With hdrTemp.PageNumbers
 .Add PageNumberAlignment:=wdAlignPageNumberCenter
 .NumberStyle = wdPageNumberStyleArabic
 .IncludeChapterNumber = True
 .HeadingLevelForChapter = 0
 .ChapterPageSeparator = wdSeparatorEnDash
End With
End Sub
Gabrielle
  • 23
  • 4
  • 1
    For how to set up numbering correctly see: https://shaunakelly.com/word/numbering/numbering20072010.html For chapter numbers see item 4 on this page: https://shaunakelly.com/word/numbering/usebuiltinheadingstyles.html – Timothy Rylatt Mar 16 '23 at 11:41

1 Answers1

0

Thanks to Timothy Rylatt, who suggested I look at Shauna Kelly's article on built-in heading styles, I have the answer to my question. According to Shauna, you must use the built-in Heading styles if you want to include chapter numbering in the Table of Contents, and doing this will use the number formatting defined for the heading level. I set Heading 8 and Heading 9 as Appendix levels 1 and 2, and by creating a TofC based on styles (Heading 8 and 9) and setting them to Outline levels 1 and 2 respectively, they appeared in the Table of Contents at the appropriate levels. The page numbers used a letter for the chapter (e.g. A-1).

The only problem now is to have them appear in the correct position in the navigation pane and to link the remaining Appendix styles to the numbering of the Heading styles, since setting the starting number, inserts superfluous headings into the text. But those are different issues. (Oddly, I was able to set the outline level for Heading 8 and 9 to levels 1 and 2 in a document, fixing the navigation pane problem, but when I tried to put that in the template, it didn't work: the list style overrode the outline level returning them to 8 and 9.) But thank you, Timothy for resolving the main issue.

Gabrielle
  • 23
  • 4