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.
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.