View Single Post
 
Old 05-29-2018, 03:39 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

WOW, that was fast! Thank you!

I've got the forms code in no problems but when I amended the printout statement and run it, I get a Run-time error '13': Type mismatch dialog.

I've highlighted the line that 'debug' doesn't seem to like.

This is the module code
Code:
Sub PrintNumberedCopiesSelect()
'
' PrintNumberedCopiesSelectionofPages macro
' Shortcut keys Alt+S
'

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 ufrmPrintNumberedCopiesSelect
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 ufrmPrintNumberedCopiesSelect
    
    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, pages:=my_form.txtPages
     Next
    
    
    ' save the next starting serial number
    ActiveDocument.Variables(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
Reply With Quote