View Single Post
 
Old 05-29-2018, 01:31 AM
kiwimtnbkr kiwimtnbkr is offline Windows 10 Office 2010 64bit
Advanced Beginner
 
Join Date: Oct 2017
Posts: 69
kiwimtnbkr is on a distinguished road
Default

Absolutely not a worry with the delay in your reply, was kind of expecting it - and the wait was worth it!

After a very minor change to three lines of your code (highlighted below) it most certainly works at home so massive thanks. Will try tomorrow at work and see what happens.

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 = "hp deskjet 5550 series (HPA) 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.Variables(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.Variables(cdp_name) = CStr(copy_number)
         ActiveDocument.PrintOut copies:=1
     Next
    
    
    ' save the next starting serial number
    ActiveDocument.Variables(cdp_name) = last_copy + 1
    
 ' snipped as the remainder of the code is not changed
Another question (and one I should have asked right from the outset - sorry!) is that there is the need to be able to just print certain pages of the document but retaining the ability of being able to specify the number of copies required and the starting serial number.

This is the code that was being used:
Code:
Sub PrintNumberedCopiesSelectionofPages()
'
' PrintNumberedCopiesSelectionofPages Macro
' Shortcut Key Alt+S
'
Dim Msg As String, Ans As Long
    Dim lCopiesToPrint As Long
    Dim lCounter As Long
    Dim lCopyNumFrom As Long
    Dim strPages As String
        
     Ans = MsgBox(Space(1) & "Is the document print settings configured for 'Print on Both Sides?" & vbCrLf & vbCrLf & _
        "If not then click 'No', click the 'File' tab, 'Print', select 'Print on Both Sides / Flip pages on long edge' and then press 'Alt+S'.", _
        vbMsgBoxSetForeground + vbQuestion + vbYesNoCancel, (Space(50) & "ABCDF 1234" & Application.Name))
   
       Select Case Ans
             
        Case vbYes
        Case vbNo
        End
        Case vbCancel
        End
        
End Select

    ' ask how many to print
    On Error GoTo Canceled
    lCopiesToPrint = InputBox( _
        prompt:="How many copies do you require?", _
        Title:=(Space(45) & "ABCDF 1234"), _
        Default:="1")
 
    ' ask where to start numbering
    On Error GoTo Canceled
    lCopyNumFrom = InputBox( _
        prompt:="Number at which to start numbering copies?", _
        Title:=(Space(45) & "ABCDF 1234"), _
        Default:=CStr(ActiveDocument.Variables("CopyNum") + 1))
        
    ' ask what pages need printing
      On Error GoTo Canceled
      strPages = InputBox( _
         prompt:="What pages require printing?", _
         Title:=(Space(45) & "ABCDF 1234"), _
         Default:="1-4")
      
    ' loop through the print-write-print cycle
    For lCounter = 0 To lCopiesToPrint - 1
        ' update the document variable
        ActiveDocument.Variables("CopyNum") = _
            lCopyNumFrom + lCounter
                With Options
                    ' .UpdateFieldsAtPrint = False
                    .UpdateLinksAtPrint = True
                End With
                    ActiveDocument.Fields.Update
        ' print this numbered copy
         ActiveDocument.PrintOut Range:=wdPrintRangeOfPages, Pages:=strPages
         
    Next lCounter
 
Canceled:
 
End Sub
I have created another userform (ufrmPrintNumberedCopiesSelect) and incorporated frame 3 with the caption 'What pages require printing?' and textbox3 to read txtPages.

Screenshot 2018-05-29 20.19.15.png

One sticking point I have when I modified the code from ufrmPrintNumberedCopies is that it doesn't recognise the hyphens for the page range...

Code:
If Not IsNumeric(Trim(Me.txtPages)) Then
        MsgBox "The pages required should be a number", vbOKOnly
        Me.txtPages.SetFocus
        Me.txtPages.SelLength = Len(Me.txtPages)
        form_validates_ok = False
        Exit Function
    End If
Will it be easy in a new module to add to your previously written code the ability to print just certain pages?
Reply With Quote