Hello
I've a button to try clean the oldest job in a selected printer . The code doesn't return any error , it always returns "Oldest print job canceled for printer: " & selectedPrinter
But when i go to print management , the job remains there , i can clean in the print management withou any issue
I've checking if it is permissions ou any other problem , but i can't find any problem
Is there any way to make the code to clean at least the oldest job or even better all the jobs in a selected printer ?
In the selected printer is defined : \\printerserver\printername
Thank you in advance
I've a button to try clean the oldest job in a selected printer . The code doesn't return any error , it always returns "Oldest print job canceled for printer: " & selectedPrinter
But when i go to print management , the job remains there , i can clean in the print management withou any issue
I've checking if it is permissions ou any other problem , but i can't find any problem
Is there any way to make the code to clean at least the oldest job or even better all the jobs in a selected printer ?
In the selected printer is defined : \\printerserver\printername
VBA Code:
Private Sub CancelOldestJob(printerName As String)
On Error GoTo ErrorHandler
Dim hPrinter As Long
Dim pcbNeeded As Long
Dim pcReturned As Long
Dim pJob As PRJ_INFO_1
Dim i As Long
Dim oldestJobTime As Date
Dim oldestJobId As Long
Dim jobTime As Date
' Open the printer
If OpenPrinter(printerName, hPrinter, 0) Then
' Enumerate the print jobs
If EnumJobs(hPrinter, 0, -1, 1, ByVal 0&, 0, pcbNeeded, pcReturned) Then
If pcbNeeded > 0 Then
Dim buffer() As Byte
ReDim buffer(pcbNeeded - 1)
If EnumJobs(hPrinter, 0, -1, 1, buffer(0), pcbNeeded, pcbNeeded, pcReturned) Then
oldestJobTime = Now ' Initialize with current time
For i = 0 To pcReturned - 1
' Copy the bytes from the buffer to the pJob structure
CopyMemory ByVal VarPtr(pJob), buffer(i * LenB(pJob)), LenB(pJob)
' Get the submission time of the job
jobTime = DateAdd("s", pJob.Submitted, #1/1/1970#)
' Check if this job is older than the current oldest job
If jobTime < oldestJobTime Then
oldestJobTime = jobTime
oldestJobId = pJob.JobId
End If
Next i
' Cancel the oldest job if found
If oldestJobId <> 0 Then
SetJob hPrinter, oldestJobId, 1, ByVal 0&, JOB_CONTROL_CANCEL
End If
End If
End If
End If
' Close the printer
ClosePrinter hPrinter
End If
Exit Sub
ErrorHandler:
MsgBox "An error occurred while canceling print job: " & Err.Description, vbExclamation, "Error"
End Sub
VBA Code:
Private Sub CancelOldestJobButton_Click()
Dim selectedPrinter As String
' Get the selected printer from the listbox or textbox
selectedPrinter = "\\" & TextBox4.value & "\" & TextBox16.value ' or TextBox1.Value
' Call the function to cancel the oldest print job for the selected printer
CancelOldestJob selectedPrinter
MsgBox "Oldest print job canceled for printer: " & selectedPrinter, vbInformation, "Print Job Canceled"
End Sub
Thank you in advance