Saturday, 12 February 2022

Page X of Y in Microsoft Publisher

Microsoft Publisher has always been the ‘ugly duckling’ of the Microsoft Office Suite. It doesn’t have the same prominence as other Office programs.  Unlike Microsoft Word you cannot create a table of contents that updates automatically for example. Nevertheless, if you want to mix text and graphics is a little easier with Publisher than with Word.

I used Microsoft Publisher to edit our club magazine, The Rovers Document.  Manually editing the table of contents (TOC)  was not the biggest bugbear. I wanted to have the footer on each page say ‘The Rovers Document’ on the left-hand side, with Page X of Y in the centre and the month and year at the right hand side. X being the page number and Y being the total number of pages.

There is no inbuilt way to put Page X of Y using Publisher so I looked for a way of doing this using VBA. A web search yielded a macro that adds a text box to every page with the page number and page count. That is not very satisfactory for a number of reasons. It is difficult to update and it does not use the header or footer.

Publisher uses master pages as templates to contain items that should appear on more than one page. In particular they contain header and footer elements. If you want to put a page number marker in a header or a footer you insert a page number marker by pressing Alt+Shift+9.

The marker displays as a ‘#’ character but, of course, it isn’t. If you use ‘#’ it does not work but if you use VBA to try to read that character it reads a ’#’.  Publisher VBA is not well documented, lacks a macro recording ability and there is no sendkeys facility. So how do you program placing a page number marker in the footer exactly where you want it?

So start on one of the pages where the footer has to appear (this makes sure that you access the correct master page) and run the macro. ActiveDocument.MasterPages(2).Footer in my publication is the footer on the master page used as a template for all pages except the TOC. Note that I have started a new section after the TOC.

strPagesCount is the count of pages, reduced by one to exclude the TOC. Then strIssue is the Month and Year converted to string format.

This is an updated version of the macro because I discovered that ~.TextRange.InsertPageNumber inserts a page number marker after the text, meaning that I do not need to use the CommandBars route to place a page number marker. I simply set the text, insert the page number marker and add the page number count and the issue date at the end.

Sub NumberPages()
    Dim strPagesCount As String'The number of pages
    Dim strIssue As String 'The month and year 
    Const conDocName As String = "The Rovers Document" 'The document name to be placed in the footer
    strPagesCount = CStr(ActiveDocument.Pages.Count - 1) 'Subtract 1 because the TOC is not to be included
    strIssue = CStr(Format(Date, "mmmm yyyy")) 'Find the current month and year
    With ActiveDocument.MasterPages(2).Footer.TextRange
        .Text = conDocName & vbTab & "Page " '(Re)sets the footer text
        .InsertPageNumber 'Insert a page number marker
        .InsertAfter (" of  " & strPagesCount & vbTab & strIssue) 'Insert text following the page number
    End With
End Sub

No comments:

Post a Comment