PDA

View Full Version : Cancel VBA print procedure



gmaxey
06-16-2011, 04:43 AM
I can't seem to shake the cobwebs and see a solution to the following.

I have a VBA procedure called with a UserForm command button to print copies of a document. The UserForm passes a start and end date to the procedure which resolves a total number of copies to print. Notification of the printing process is displayed via label caption on the Userform as the printing is accomplished.

Sub PrintDatedCopies(ByRef dStart As Date, dEnd As Date)
Dim oDateStart As Date
Dim oDateEnd As Date
Dim oDateCopy As Date
Dim lngCopies As Long
Dim Counter As Long
Dim bPrint As Boolean
oDateStart = dStart
oDateEnd = dEnd
lngCopies = DateDiff("d", oDateStart, oDateEnd) + 1
Counter = 0
If MsgBox("Click ""OK"" to confirm and print " & lngCopies & " sequentially dated copies of this document", vbInformation + vbOKCancel, "CONFIRM PRINT") = vbOK Then
While Counter < lngCopies
oDateCopy = DateAdd("d", Counter, oDateStart)
ActiveDocument.Variables("CopyDate").Value = oDateCopy
ActiveDocument.Fields.Update
frmPrintDate.lblInfo = "Printing copy " & Counter + 1 & ": " & oDateCopy
Debug.Print oDateCopy
'ActiveDocument.PrintOut
Counter = Counter + 1
Wend
End If
ActiveDocument.Variables("CopyDate").Value = oDateStart
ActiveDocument.Fields.Update
End Sub

I want to revise my Userform cmd button (caption "Print") click procedure:

Private Sub cmdOK_Click()
If IsDate(Me.txtStartDate) And IsDate(Me.txtEndDate) Then
If CDate(Me.txtStartDate) < CDate(Me.txtEndDate) Then
PrintDatedCopies CDate(Me.txtStartDate), CDate(Me.txtEndDate)
Else
With Me.lblInfo
.Caption = "The end date is before the start date."
.ForeColor = wdColorRed
End With
Exit Sub
End If
Else
With Me.lblInfo
.Caption = "One or more input fields is not a valid date."
.ForeColor = wdColorRed
End With
Exit Sub
End If
Unload Me
End Sub


So that the caption changes from "Print" to "Cancel Printing" after the print process is started. The idea being if the user then clicks "Cancel Printing" the printing operations are stopped.

Changing the cmd button caption is not a problem. I thought maybe I could change the lngCopies and Counter variables to Public and then if "Cancel Printing" was clicked I would use the cmd btn click event to set Counter = lngCopies and this satisfiy the loop. The problem is that once the call to the external print procedure is made clicking "Cancel Print" on the userform has no effect. This confirms that my thoughts were fruitless if not a bit silly but as I say I can't shake the cobwebs.

Hopefully I have made the goal clear. Anyone have an idea on how or if it is achieveable? Thanks.

gmaxey
06-16-2011, 05:17 AM
DoEvents

Dooh!! Never mind.