I am trying to use the code
here to sequentially number multiple copies of a one-page document, with a twist. Rather than adding the code to every document to be numbered, I wanted to add the code to an add-in. To that end, I have the following code:
Code:
Public Sub PageSeqNum()
'---------------------------------------------------------------------------------------
' Procedure : PageSeqNum
' Author : BruceM, from code by Doug Robbins at word.mvps.org
' Date : 27-Feb-2015
' Purpose : Prints a single page the number of times specified, with pages _
sequentially numbered in "Page x of y" format
' Comments : Works on a single page document only
'---------------------------------------------------------------------------------------
Const msgPgNum As String = _
"Enter the number of copies you want to print"
Const ttlPgNum As String = _
"Print Copies"
Const strDefault As String = _
"1"
Const strBkMk As String = _
"PageNum"
Dim lngNum As Long ' Number of copies to be printed (from input box)
Dim lngCur As Long ' Number of current page
Dim rngBkMk As Range ' The Range of the SeqNumber bookmark
Dim strPrinter As String ' The name of the active printer
On Error GoTo PageSeqNum_Error
strPrinter = _
ActivePrinter
' Set the printer for duplex printing
SetPrinterDuplex _
PrinterName:=strPrinter, _
DuplexSetting:=2 ' More on this in the thread
Set rngBkMk = _
ActiveDocument.Bookmarks(strBkMk).Range
lngNum = _
Val(InputBox(msgPgNum, ttlPgNum, strDefault))
lngCur = _
1
While lngCur <= lngNum
With rngBkMk
.Delete
.Text = "Page " & lngCur & " of " & lngNum
End With
ActiveDocument.PrintOut
lngCur = _
lngCur + 1
Wend
ProcExit:
' Re-create the bookmark for next time
ActiveDocument.Bookmarks.Add _
Name:=strBkMk, _
Range:=rngBkMk
' Reset the printer duplex setting to its original (lngOldDuplex is set within SetPrinterDuplex _
when it is called at the beginning of this procedure)
SetPrinterDuplex _
PrinterName:=strPrinter, _
DuplexSetting:=lngOldDuplex
Exit Sub
PageSeqNum_Error:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") " & _
"in PageSeqNum, basPrintPages"
Resume ProcExit
End Sub
Regarding the duplex setting, that is an attempt to set the printer to duplex printing using code from
here (kb 230743). Duplex is needed because this system is used to print things such as maintenance logs that are bound into books. The printer supports duplex.
The general idea is that I insert a bookmark named PageNum into the footer of the document to be sequentially numbered, then run the PageSeqNum code via a ribbon icon (that is, run the macro that is in the add-in, in a way that is simple to do).
The code for sequential numbering works well enough, except I cannot find a way to make it print on both sides of the page, even when I set the printer to duplex as its default setting and/or set two-sided printing for the document.
Brief comment about the code to set duplex: The plan is to detect the original settings (typically one-sided, but not always), and restore that setting at the end. I haven't worked out the details yet, and I may have some questions about that later, but for now I am just trying to solve the basic problem of two-sided printing for this situation. It makes no difference if I omit the procedure calls from the code posted above.
If this cannot be made to work we will revert to the old system of copying the original one-page document and paste to a new page, then copy the whole document (two pages now) and paste four more times (ten total), and then the whole thing again ten times to arrive at a hundred pages (the typical number that needs to be printed). Something like that.
I have been fighting with this all day, and the work day is almost over. I should have posted sooner, but I mention this now so as not to seem rude if there is a delay before I respond to any suggestions.