Sorry for the delay in responding.
The code to be inserted in ufrmPrintNumberedCopies is below.
Code:
Option Explicit
Private self_result As Boolean
Private Sub UserForm_Initialize()
Me.StartUpPosition = 0
Me.Top = Application.Top + (0.5 * Application.Height) - (Me.Height / 2)
Me.Left = Application.Left + (0.5 * Application.width) - (Me.width / 2)
End Sub
Private Sub cmdCancel_Click()
self_result = False
Me.Hide
End Sub
Private Sub cmdOK_Click()
If form_validates_ok Then
self_result = True
Me.Hide
Exit Sub
End If
End Sub
Public Property Get Result() As Boolean
Result = self_result
End Property
Private Function form_validates_ok() As Boolean
If Not IsNumeric(Trim(Me.txtCopies)) Then
MsgBox "The number of copies should be a number", vbOKOnly
Me.txtCopies.SetFocus
Me.txtCopies.SelLength = Len(Me.txtCopies)
form_validates_ok = False
Exit Function
End If
If Not IsNumeric(Trim(Me.txtStartNumber)) Then
MsgBox "The start number should be a number", vbOKOnly
Me.txtStartNumber.SetFocus
Me.txtStartNumber.SelLength = Len(Me.txtStartNumber)
form_validates_ok = False
Exit Function
End If
form_validates_ok = True
End Function
Add the code below to a normal module. It replaces the code you posted above.
Code:
Sub PrintNumberedCopiesEntireDocument()
'
' PrintNumberedCopiesEntireDocument Macro
' Shortcut keys Alt+E
'
Const cdp_name As String = "CopyNum"
Const default_copies As String = "1"
Const duplex_printer As String = "Dell Printer 5100cn PS Duplex" ' change this to the name of your duplex printer
Dim my_form As ufrmPrintNumberedCopies
Dim copy_number As Long
Dim first_copy As Long
Dim last_copy As Long
Dim my_update_fields_at_print As Boolean
Dim my_update_link_at_print As Boolean
Dim current_printer As String
' We are using a custom Document Property to save the serial number
' as we can edit this from the backstage properties advanced dialog
' if needed
ensure_cdp_exists cdp_name
Set my_form = New ufrmPrintNumberedCopies
With my_form
.txtStartNumber = Trim(ActiveDocument.CustomDocumentProperties(cdp_name))
.txtCopies = default_copies
.Show
If Not .Result Then
Exit Sub
End If
End With
' We now switch to the document printer that has been customised to aleays
' print in duplex
current_printer = ActivePrinter
ActivePrinter = duplex_printer
' The update fields and links at print time are application properties so anly need setting once
' but we preserve the values so we can restore the current settings after we have finished printing
With Options
my_update_fields_at_print = .UpdateFieldsAtPrint
.UpdateFieldsAtPrint = True
my_update_link_at_print = .UpdateLinksAtPrint
.UpdateLinksAtPrint = True
End With
first_copy = CLng(Trim(my_form.txtStartNumber))
last_copy = first_copy + CLng(Trim(my_form.txtCopies)) - 1
MsgBox "Now we do the printing", vbOKOnly 'comment this out once tested
' it is assumed that there is a field somewhere that references the customdocumentproperty
' For copy_number = first_copy To last_copy ' uncomment the for loop once tested
' ActiveDocument.CustomDocumentProperties(cdp_name) = Cstr(copy_number)
' ActiveDocument.PrintOut copies:=1
' Next
' save the next starting serial number
ActiveDocument.CustomDocumentProperties(cdp_name) = last_copy + 1
' restore saved settings
With Options
.UpdateFieldsAtPrint = my_update_fields_at_print
.UpdateLinksAtPrint = my_update_link_at_print
End With
ActivePrinter = current_printer
End Sub
Sub ensure_cdp_exists(cdp_name As String)
'cdp is short for CustomDocumentProperty
' Searches for the a custom document property of cdp_name
' If not found the custom document property is created and assigned a value of 1
Const default_start_number As String = "1"
Dim my_cdp As DocumentProperty
For Each my_cdp In ActiveDocument.CustomDocumentProperties
If my_cdp.name = cdp_name Then
Exit Sub
End If
Next
' We only get to this point if we don't find the target custom document property
' using the previous loop
ActiveDocument.CustomDocumentProperties.Add _
name:=cdp_name, _
LinkToContent:=False, _
Value:=default_start_number, _
Type:=msoPropertyTypeString
' Be gentle with the user and let them know what has happened.
MsgBox _
Title:="Missing CustomDocumentProperty", _
prompt:="CustomDocumentProperty '" & cdp_name & "' was added to the document" & vbCrLf & vbCrLf & "A value of '" & default_start_number & "' was assigned'", _
Buttons:=vbOKOnly
End Sub
With the above code there is good news and bad news.
The bad news is that it seems virtually impossible to set duplex printing in word via VBA.
I've tried the PCL 5 trick of embedding a print field in the header but this fails with PCL 6.
I've tried using sendkeys but for whatever reason the specific button on the print backstage tab cannot be selected in this way.
I've also tried adapting code from here
https://support.microsoft.com/en-us/...mation-clients
and whilst I have successfully got the code to run in a 64 bit system, actually getting Duplex printing has eluded me.
The good news is that all of the above can be avoided by installing a dedicated duplex printer.
You do this be reinstalling the current printer, saying yes to use the current printer driver, and then just add duplex to the end of the name in the final part of the dialog box.
You can then access the printers properties and set the defaults for whatever you want and fortunately, duplex printing is one of these options (if it is supported by the printer).
It is then very simple code to switch the active printer in VBA to use the duplex printer (as demonstrated above).
Let us know if you have any problems with the code above.