Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-29-2018, 07:14 AM
gmaxey gmaxey is offline SENDKEYS with vbYesNoCancel and Select Case Windows 7 32bit SENDKEYS with vbYesNoCancel and Select Case Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,617
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
  #2  
Old 05-29-2018, 08:22 AM
slaycock slaycock is offline SENDKEYS with vbYesNoCancel and Select Case Windows 7 64bit SENDKEYS with vbYesNoCancel and Select Case Office 2016
Expert
 
Join Date: Sep 2013
Posts: 255
slaycock is on a distinguished road
Default

Greg

Two reasons

1. Ignorance (mea culpa). I am a self taught VBA'er so probably miss out on lots that could make my life easier as I don't realise what the alternatives are until I've been forced into a position where I have to explore alternatives.

2. Trying not to overload the OP ( a clutching a straws excuse).

3. Trying to make the process straighforward for the OP or anyone else who has limited experience at VBA.
Reply With Quote
  #3  
Old 05-29-2018, 07:57 PM
kiwimtnbkr kiwimtnbkr is offline SENDKEYS with vbYesNoCancel and Select Case Windows 10 SENDKEYS with vbYesNoCancel and Select Case Office 2010 64bit
Advanced Beginner
SENDKEYS with vbYesNoCancel and Select Case
 
Join Date: Oct 2017
Posts: 69
kiwimtnbkr is on a distinguished road
Default

hi Greg,

Thanks for the code but it throws a 'Compile error: User-defined type not defined" as soon as it gets to

Dim oFrm As frmPrintDialog
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Case and Select case brent chadwick Word VBA 34 10-18-2015 02:13 PM
Replace & case Jennifer Murphy Word 1 02-11-2013 03:26 AM
SENDKEYS with vbYesNoCancel and Select Case A macro that can insert FILENAME, sendkeys CTRL ALT T, paste clipboard, and nextline kyjac85 Word VBA 13 09-20-2012 05:00 PM
Problem with the sendkeys in Win7 vidyapakki Excel Programming 1 05-07-2012 11:10 PM
SENDKEYS with vbYesNoCancel and Select Case From all UPPER CASE to Proper Case davers Word 1 04-30-2009 12:41 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:01 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft