View Single Post
 
Old 05-29-2018, 07:14 AM
gmaxey gmaxey is offline Windows 7 32bit Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,598
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Gents,

It is a slow day and raining outside. I hope neither of you will mind if I chime in with a few alternatives and questions. Each of us has (or will develop) their own style. In the code below, I've used mine.

The form code:

Code:
Option Explicit
Dim m_bTest As Boolean
Private Sub txtStartNumber_Change()
  Validate_OK
End Sub
Private Sub txtStartNumber_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  m_bTest = IsInteger(KeyAscii)
  If m_bTest = False Then
    Beep
    KeyAscii = 0
  End If
End Sub
Private Sub txtCopies_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  m_bTest = IsInteger(KeyAscii)
  If m_bTest = False Then
    Beep
    KeyAscii = 0
  End If
End Sub
Private Sub txtCopies_Change()
  Validate_OK
End Sub
Function IsInteger(ByVal i As String) As Boolean
  Select Case i  'Checking to see if inside valid Ascii range for integers
    Case 48 To 57: IsInteger = True
    Case Else: IsInteger = False
  End Select
End Function
Private Sub UserForm_Activate()
  Validate_OK
End Sub
Private Sub cmdCancel_Click()
  Tag = "CANCX"
  Hide
End Sub
Private Sub cmdOK_Click()
  Hide
End Sub
Private Sub Validate_OK()
  cmdOK.Enabled = True
  If Not IsNumeric(Trim(txtCopies)) Then cmdOK.Enabled = False
  If Not IsNumeric(Trim(txtStartNumber)) Then cmdOK.Enabled = False
End Sub
The standard module code:

Code:
Option Explicit
Sub PrintNumberedCopiesSelect()
Const strCDP_name As String = "CopyNum"
Const strDuplex_printer As String = "hp deskjet 5550 series (HPA) duplex"  ' change this to the name of your duplex printer
Dim oFrm As frmPrintDialog
Dim lngCopy_number As Long, lngFirst_copy As Long, lngLast_copy As Long
Dim bUpdate_fields_at_print As Boolean, bUpdate_link_at_print As Boolean
Dim strCurrent_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.
  Validate_CDP strCDP_name
  Set oFrm = New frmPrintDialog
  With oFrm
    .txtStartNumber = Trim(ActiveDocument.CustomDocumentProperties(strCDP_name))
    .txtCopies = "1"
    .Show
    If .Tag = "CANCX" Then GoTo lbl_Exit
  End With
  'We now switch to the document printer that has been customised to aleays print in duplex.
  strCurrent_printer = ActivePrinter
  ActivePrinter = strDuplex_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
    bUpdate_fields_at_print = .UpdateFieldsAtPrint
    .UpdateFieldsAtPrint = True
    bUpdate_link_at_print = .UpdateLinksAtPrint
    .UpdateLinksAtPrint = True
  End With
  lngFirst_copy = CLng(Trim(oFrm.txtStartNumber))
  lngLast_copy = lngFirst_copy + CLng(Trim(oFrm.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 lngCopy_number = lngFirst_copy To lngLast_copy ' uncomment the for loop once tested
    ActiveDocument.Variables(strCDP_name) = CStr(lngCopy_number)
    ActiveDocument.PrintOut copies:=1, Pages:=oFrm.txtPages.Value
  Next
  'Save the next starting serial number
  ActiveDocument.CustomDocumentProperties(strCDP_name) = lngLast_copy + 1
  'Restore saved settings
  With Options
    .UpdateFieldsAtPrint = bUpdate_fields_at_print
    .UpdateLinksAtPrint = bUpdate_link_at_print
  End With
  ActivePrinter = strCurrent_printer
lbl_Exit:
  Unload oFrm
  Set oFrm = Nothing
End Sub
Sub Validate_CDP(strCDP_name As String)
Dim oCDP As DocumentProperty
  'See if custom document property already exists.
  On Error Resume Next
  Set oCDP = ActiveDocument.CustomDocumentProperties(strCDP_name)
  If Not oCDP Is Nothing Then Exit Sub
  On Error GoTo 0
  'It doesn't exists so create it.
  ActiveDocument.CustomDocumentProperties.Add Name:=strCDP_name, _
                LinkToContent:=False, Value:="1", Type:=msoPropertyTypeString
  MsgBox "A CustomDocumentProperty named """ & strCDP_name & """ with the value ""1"" was added to the document.", _
         vbInformation + vbOKOnly, "Create CustomDocumentProperty"
lbl_Exit:
  Exit Sub
End Sub
In the code above, the loop check for the custom document property is replaced with a process of trying to access the item explicitly. The command button validation is upfront. It can't be clicked if not valid.

Slaycock are there reasons (which I may be unaware of) why you use the public property "Result" rather than the built-in Tag property of the Userform? Is there a reason you set the form to the display top left then move to center vice using the default Center Owner?

Thanks
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/

Last edited by gmaxey; 05-29-2018 at 06:40 PM.
Reply With Quote