0

Recently I have been struggling with publisher at work, and the most recent source of concern is the software inability to give something a simple as an auto-updating "total pages" count.

I decided to look into macros for a solution, but with my limited knowledge of VBA I only managed to get so fat before hitting a roadblock.

I want to create a macro that automatically starts when the document is opened and as frequently as possible updates a textbox in the master page. The textbox should show "page X of Y", but due to publisher limitations only the "X" is automatically updated without using a macro.

What I have right now:

Private WithEvents PubApp As Publisher.Application

Private Sub Document_Open()
 Set PubApp = Publisher.Application
End Sub

Private Sub PubApp_WindowPageChange(ByVal Vw As View)
 MsgBox "Your publication contains " & _
 ActiveDocument.Pages.Count & " page(s)."
End Sub

The macro automatically starts when the document is opened and creates a popup with the total page number every time the user changes page.

So, I accomplished the first half of my goal, but I need some help with the rest.

shA.t
  • 16,580
  • 5
  • 54
  • 111
M.Baroni
  • 21
  • 6
  • I think your question is about ms-publisher instead of publisher tag, And I want to know that your VBA code is running in ms-publisher ? – shA.t Apr 25 '15 at 05:57

2 Answers2

1

Here's a working macro for anyone with the same problems

Private WithEvents PubApp As Publisher.Application

Private Sub Document_Open()
 Set PubApp = Publisher.Application
End Sub

Private Sub PubApp_WindowPageChange(ByVal Vw As View)

  Dim mp As MasterPages
  Set mp = ActiveDocument.MasterPages

  With mp.Item(1)
   .Shapes(19).TextFrame.TextRange.Text = ""
   .Shapes(19).TextFrame.TextRange.InsertPageNumber
   .Shapes(19).TextFrame.TextRange.InsertBefore _
            NewText:="Page "
    If ActiveDocument.Pages.Count > 9 Then
        .Shapes(19).TextFrame.TextRange.InsertAfter _
            NewText:=" of " & _
            ActiveDocument.Pages.Count
    Else
        .Shapes(19).TextFrame.TextRange.InsertAfter _
            NewText:=" of 0" & _
            ActiveDocument.Pages.Count
    End If
  End With

End Sub

X In mp.Item(X) identifies your master page (in case you have more than one).

Y in Shapes(Y) identifies the target text box.

The macro runs in background without the need of active input and refreshes the content of the target text box on every page change.

Since the automatic page numbering format used in the document is "01, 02, 03 ... 11, 12 13" I included an If selection to add a 0 before the total page count if said number is lower than 10.

I'm sure there are more elegant ways of solving this problems, but hey, at least it works.

shA.t
  • 16,580
  • 5
  • 54
  • 111
M.Baroni
  • 21
  • 6
0

Here is a clarification for anyone else trying to research the limited information on programmatically changing the header or footer in the master pages using VBA in Publisher. This tip also includes a clarification of how to Add Text To the Left,Center,Right portions of the header:

All the credit goes to M.Baroni

Function FixHeader1()
  Dim mp As MasterPages
  Set mp = ActiveDocument.MasterPages
  With mp.Item(1)
   .Header.TextRange.text = "My Publication" & vbTab
   .Header.TextRange.InsertPageNumber
   .Header.TextRange.InsertAfter NewText:=vbTab & MonthName(Month(Date)) & " " & Year(Date)
  End With
End Function
Mickey D
  • 347
  • 2
  • 12