VBA Button to clean printer jobs on a selected printer with no errors but it won't delete

malveiro

New Member
Joined
May 13, 2015
Messages
30
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

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
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Post the whole code, including Windows API declarations and data types.
Hello John , thanks for the reply :

Here it is

VBA Code:
Private Declare PtrSafe Function ShellExecuteEx Lib "shell32.dll" _
Alias "ShellExecuteExA" (ByRef lpExecInfo As SHELLEXECUTEINFO) As LongPtr

#If VBA7 Then
    Private Declare PtrSafe Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (ByRef lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUsername As String, ByVal dwFlags As Long) As Long
    Private Declare PtrSafe Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long
    Private Declare PtrSafe Function SetDefaultPrinter Lib "winspool.drv" Alias "SetDefaultPrinterA" (ByVal pszPrinter As String) As Long
    Private Declare PtrSafe Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As LongPtr, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongPtr
    Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
     Private Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
#Else
    Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (ByRef lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUsername As String, ByVal dwFlags As Long) As Long
    Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long
    Private Declare Function SetDefaultPrinter Lib "winspool.drv" Alias "SetDefaultPrinterA" (ByVal pszPrinter As String) As Long
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

#End If

Private Declare PtrSafe Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, ByVal pDefault As Long) As Long
Private Declare PtrSafe Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Private Declare PtrSafe Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" (ByVal hPrinter As Long, ByVal FirstJob As Long, ByVal NoJobs As Long, ByVal Level As Long, pJob As Any, ByVal cdBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long
Private Declare PtrSafe Function SetJob Lib "winspool.drv" Alias "SetJobA" (ByVal hPrinter As Long, ByVal JobId As Long, ByVal Level As Long, pJob As Any, ByVal Command As Long) As Long
Private Declare PtrSafe Function CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Type SYSTEMTIME
    wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Integer
End Type

Private Const JOB_CONTROL_CANCEL = 3
Private Const PRINTER_ACCESS_USE = &H8
Private Const PRINTER_ALL_ACCESS = &HF0000 Or &H1 Or &H2 Or &H4 Or &H8 Or &H10 Or &H20 Or &H40 Or &H80 Or &H100 Or &H200 Or &H400 Or &H800 Or &H1000

Private Type PRJ_INFO_1
    JobId As Long
    pPrinterName As String
    pMachineName As String
    pUserName As String
    pDocument As String
    pDatatype As String
    pStatus As String
    Status As Long
    Priority As Long
    Position As Long
    StartTime As Long
    UntilTime As Long
    TotalPages As Long
    Size As Long
    Submitted As Long
End Type
 
Upvote 0
I didn't use any of your code.

Code in a standard module, which defines the public routine Delete_All_Printer_Jobs.

VBA Code:
Option Explicit

Private Type SYSTEMTIME
    wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Integer
End Type

Private Type JOB_INFO_1
    JobId As Long
    pPrinterName As LongPtr
    pMachineName As LongPtr
    pUserName As LongPtr
    pDocument As LongPtr
    pDatatype As LongPtr
    pStatus As LongPtr
    Status As Long
    Priority As Long
    Position As Long
    TotalPages As Long
    PagesPrinted As Long
    Submitted As SYSTEMTIME
End Type

Private Const JOB_CONTROL_PAUSE = 1
Private Const JOB_CONTROL_RESUME = 2
Private Const JOB_CONTROL_CANCEL = 3
Private Const JOB_CONTROL_RESTART = 4
Private Const JOB_CONTROL_DELETE = 5

#If VBA7 Then

    Private Declare PtrSafe Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As LongPtr, pDefault As Any) As Long
    Private Declare PtrSafe Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As LongPtr) As Long
    Private Declare PtrSafe Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" (ByVal hPrinter As LongPtr, ByVal FirstJob As Long, ByVal NoJobs As Long, ByVal Level As Long, ByVal pJob As LongPtr, ByVal cbBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long
    Private Declare PtrSafe Function SetJob Lib "winspool.drv" Alias "SetJobA" (ByVal hPrinter As LongPtr, ByVal JobId As Long, ByVal Level As Long, pJob As LongPtr, ByVal Command As Long) As Long

    Private Declare PtrSafe Function GetProfileString Lib "kernel32.dll" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
    
    Private Declare PtrSafe Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As LongPtr)
    
    Private Declare PtrSafe Function lstrlen Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As LongPtr) As Long
    Private Declare PtrSafe Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As LongPtr) As Long

    Private Declare PtrSafe Sub Sleep Lib "kernel32.dll" (ByVal ms As LongPtr)

#Else

    Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As Any) As Long
    Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
    Private Declare Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" (ByVal hPrinter As Long, ByVal FirstJob As Long, ByVal NoJobs As Long, ByVal Level As Long, ByVal pJob As Long, ByVal cbBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long
    Private Declare Function SetJob Lib "winspool.drv" Alias "SetJobA" (ByVal hPrinter As Long, ByVal JobId As Long, ByVal Level As Long, pJob As Long, ByVal Command As Long) As Long
    
    Private Declare Function GetProfileString Lib "kernel32.dll" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
    
    Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    
    Private Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As Long) As Long
    Private Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Long) As Long
   
    Private Declare Sub Sleep Lib "kernel32.dll" (ByVal ms As Long)
   
#End If


Public Sub Delete_All_Printer_Jobs(Optional PrinterName As String = "")

    'Delete all jobs on the specified printer queue, or the default printer if not specified
    
    #If VBA7 Then
        Dim printerHandle As LongPtr
    #Else
        Dim printerHandle As Long
    #End If
    Dim totalJobs As Long
    Dim JobInfoBuffer() As Byte
    Dim JobInfo() As JOB_INFO_1
    Dim pcbNeeded As Long
    Dim pcReturned As Long
    Dim job As Long
    Dim retVal As Long
    Dim st As SYSTEMTIME
    Dim submittedTime As Date
            
    'Printer not specified, so get default printer
    If PrinterName = "" Then
        PrinterName = GetDefaultPrinter
    End If
    
    'Get printer handle
    retVal = OpenPrinter(PrinterName, printerHandle, ByVal vbNullString)
    
    'Specify the total number of print jobs to enumerate
    totalJobs = 100
    ReDim JobInfo(0 To totalJobs - 1) As JOB_INFO_1

    'Get the required size of the buffer
    EnumJobs printerHandle, 0, totalJobs, 1, 0, 0, pcbNeeded, pcReturned
    
    'If a print job exists
    If pcbNeeded > 0 Then
               
        'Reserve a buffer
        ReDim JobInfoBuffer(0 To pcbNeeded - 1) As Byte
        
        'Get print job information
        EnumJobs printerHandle, 0, totalJobs, 1, VarPtr(JobInfoBuffer(0)), pcbNeeded, pcbNeeded, pcReturned
        
        'If the print job is obtained
        If pcReturned > 0 Then
        
            'Move print job information to the JOB_INFO_1 structure
            CopyMemory JobInfo(0), JobInfoBuffer(0), pcbNeeded
           
            'Extract printer jobs information and delete
            For job = 0 To pcReturned - 1
            
                With JobInfo(job)
                    
                    Debug.Print "--- CANCEL PRINTER JOB ---"
                    Debug.Print "Printer:   " & LPSTRtoVBAstring(.pPrinterName)
                    Debug.Print "Document:  " & LPSTRtoVBAstring(.pDocument)
                    Debug.Print "Status:    " & .Status
                    st = .Submitted
                    submittedTime = DateSerial(st.wYear, st.wMonth, st.wDay) + TimeSerial(st.wHour, st.wMinute, st.wSecond)
                    Debug.Print "Submitted: " & submittedTime
                    Debug.Print "Job Id:    " & .JobId
                    Debug.Print "--------------------------"
                    
                    SetJob printerHandle, .JobId, 0, 0, JOB_CONTROL_DELETE
   
                End With
                
            Next
            
        Else
        
            Debug.Print "Unable to retrieve print jobs"
            
        End If
        
    Else
    
        Debug.Print "There are no print jobs"
        
    End If
    
    ClosePrinter printerHandle
    
End Sub


'Get the default printer
'
'https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getprofilestringa
'GetProfileStringA function
'
'Retrieves the string associated with a key in the specified section of the Win.ini file.
'Note: This function is provided only for compatibility with 16-bit Windows-based applications, therefore this function should not be called from server code.
'Applications should store initialization information in the registry.

Function GetDefaultPrinter(Optional defaultPrinterName As String = "") As String

    Dim section As String
    Dim key As String
    Dim buffer As String * 1024
    
    section = "windows"
    key = "device"
    
    GetProfileString section, key, defaultPrinterName, buffer, Len(buffer)
    GetDefaultPrinter = Left(buffer, InStr(buffer, ",") - 1)
    
End Function


#If VBA7 Then
Private Function LPSTRtoVBAstring(ByVal pointerToString As LongPtr) As String
#Else
Private Function LPSTRtoVBAstring(ByVal pointerToString As Long) As String
#End If

    Dim buffer As String, ret As Long, stringLen As Long, nullCharPos As Long

    stringLen = lstrlen(pointerToString)
    buffer = String$(stringLen + 1, vbNullChar)
    ret = lstrcpy(ByVal buffer, ByVal pointerToString)
    nullCharPos = InStr(1, buffer, vbNullChar)
    If nullCharPos > 0 Then buffer = Left(buffer, nullCharPos - 1)
    'Debug.Print buffer
    LPSTRtoVBAstring = buffer
    
End Function

Call it like this from your button click routine:

VBA Code:
    Dim selectedPrinter As String

    selectedPrinter = "\\" & TextBox4.Value & "\" & TextBox16.Value ' or TextBox1.Value

    Delete_All_Printer_Jobs selectedPrinter
 
Upvote 0
I didn't use any of your code.

Code in a standard module, which defines the public routine Delete_All_Printer_Jobs.

VBA Code:
Option Explicit

Private Type SYSTEMTIME
    wYear As Integer
    wMonth As Integer
    wDayOfWeek As Integer
    wDay As Integer
    wHour As Integer
    wMinute As Integer
    wSecond As Integer
    wMilliseconds As Integer
End Type

Private Type JOB_INFO_1
    JobId As Long
    pPrinterName As LongPtr
    pMachineName As LongPtr
    pUserName As LongPtr
    pDocument As LongPtr
    pDatatype As LongPtr
    pStatus As LongPtr
    Status As Long
    Priority As Long
    Position As Long
    TotalPages As Long
    PagesPrinted As Long
    Submitted As SYSTEMTIME
End Type

Private Const JOB_CONTROL_PAUSE = 1
Private Const JOB_CONTROL_RESUME = 2
Private Const JOB_CONTROL_CANCEL = 3
Private Const JOB_CONTROL_RESTART = 4
Private Const JOB_CONTROL_DELETE = 5

#If VBA7 Then

    Private Declare PtrSafe Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As LongPtr, pDefault As Any) As Long
    Private Declare PtrSafe Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As LongPtr) As Long
    Private Declare PtrSafe Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" (ByVal hPrinter As LongPtr, ByVal FirstJob As Long, ByVal NoJobs As Long, ByVal Level As Long, ByVal pJob As LongPtr, ByVal cbBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long
    Private Declare PtrSafe Function SetJob Lib "winspool.drv" Alias "SetJobA" (ByVal hPrinter As LongPtr, ByVal JobId As Long, ByVal Level As Long, pJob As LongPtr, ByVal Command As Long) As Long

    Private Declare PtrSafe Function GetProfileString Lib "kernel32.dll" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
   
    Private Declare PtrSafe Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (pTo As Any, uFrom As Any, ByVal lSize As LongPtr)
   
    Private Declare PtrSafe Function lstrlen Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As LongPtr) As Long
    Private Declare PtrSafe Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As LongPtr) As Long

    Private Declare PtrSafe Sub Sleep Lib "kernel32.dll" (ByVal ms As LongPtr)

#Else

    Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As Any) As Long
    Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
    Private Declare Function EnumJobs Lib "winspool.drv" Alias "EnumJobsA" (ByVal hPrinter As Long, ByVal FirstJob As Long, ByVal NoJobs As Long, ByVal Level As Long, ByVal pJob As Long, ByVal cbBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long
    Private Declare Function SetJob Lib "winspool.drv" Alias "SetJobA" (ByVal hPrinter As Long, ByVal JobId As Long, ByVal Level As Long, pJob As Long, ByVal Command As Long) As Long
   
    Private Declare Function GetProfileString Lib "kernel32.dll" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
   
    Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
   
    Private Declare Function lstrlen Lib "kernel32.dll" Alias "lstrlenA" (ByVal lpString As Long) As Long
    Private Declare Function lstrcpy Lib "kernel32.dll" Alias "lstrcpyA" (ByVal lpString1 As String, ByVal lpString2 As Long) As Long
  
    Private Declare Sub Sleep Lib "kernel32.dll" (ByVal ms As Long)
  
#End If


Public Sub Delete_All_Printer_Jobs(Optional PrinterName As String = "")

    'Delete all jobs on the specified printer queue, or the default printer if not specified
   
    #If VBA7 Then
        Dim printerHandle As LongPtr
    #Else
        Dim printerHandle As Long
    #End If
    Dim totalJobs As Long
    Dim JobInfoBuffer() As Byte
    Dim JobInfo() As JOB_INFO_1
    Dim pcbNeeded As Long
    Dim pcReturned As Long
    Dim job As Long
    Dim retVal As Long
    Dim st As SYSTEMTIME
    Dim submittedTime As Date
           
    'Printer not specified, so get default printer
    If PrinterName = "" Then
        PrinterName = GetDefaultPrinter
    End If
   
    'Get printer handle
    retVal = OpenPrinter(PrinterName, printerHandle, ByVal vbNullString)
   
    'Specify the total number of print jobs to enumerate
    totalJobs = 100
    ReDim JobInfo(0 To totalJobs - 1) As JOB_INFO_1

    'Get the required size of the buffer
    EnumJobs printerHandle, 0, totalJobs, 1, 0, 0, pcbNeeded, pcReturned
   
    'If a print job exists
    If pcbNeeded > 0 Then
              
        'Reserve a buffer
        ReDim JobInfoBuffer(0 To pcbNeeded - 1) As Byte
       
        'Get print job information
        EnumJobs printerHandle, 0, totalJobs, 1, VarPtr(JobInfoBuffer(0)), pcbNeeded, pcbNeeded, pcReturned
       
        'If the print job is obtained
        If pcReturned > 0 Then
       
            'Move print job information to the JOB_INFO_1 structure
            CopyMemory JobInfo(0), JobInfoBuffer(0), pcbNeeded
          
            'Extract printer jobs information and delete
            For job = 0 To pcReturned - 1
           
                With JobInfo(job)
                   
                    Debug.Print "--- CANCEL PRINTER JOB ---"
                    Debug.Print "Printer:   " & LPSTRtoVBAstring(.pPrinterName)
                    Debug.Print "Document:  " & LPSTRtoVBAstring(.pDocument)
                    Debug.Print "Status:    " & .Status
                    st = .Submitted
                    submittedTime = DateSerial(st.wYear, st.wMonth, st.wDay) + TimeSerial(st.wHour, st.wMinute, st.wSecond)
                    Debug.Print "Submitted: " & submittedTime
                    Debug.Print "Job Id:    " & .JobId
                    Debug.Print "--------------------------"
                   
                    SetJob printerHandle, .JobId, 0, 0, JOB_CONTROL_DELETE
  
                End With
               
            Next
           
        Else
       
            Debug.Print "Unable to retrieve print jobs"
           
        End If
       
    Else
   
        Debug.Print "There are no print jobs"
       
    End If
   
    ClosePrinter printerHandle
   
End Sub


'Get the default printer
'
'https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getprofilestringa
'GetProfileStringA function
'
'Retrieves the string associated with a key in the specified section of the Win.ini file.
'Note: This function is provided only for compatibility with 16-bit Windows-based applications, therefore this function should not be called from server code.
'Applications should store initialization information in the registry.

Function GetDefaultPrinter(Optional defaultPrinterName As String = "") As String

    Dim section As String
    Dim key As String
    Dim buffer As String * 1024
   
    section = "windows"
    key = "device"
   
    GetProfileString section, key, defaultPrinterName, buffer, Len(buffer)
    GetDefaultPrinter = Left(buffer, InStr(buffer, ",") - 1)
   
End Function


#If VBA7 Then
Private Function LPSTRtoVBAstring(ByVal pointerToString As LongPtr) As String
#Else
Private Function LPSTRtoVBAstring(ByVal pointerToString As Long) As String
#End If

    Dim buffer As String, ret As Long, stringLen As Long, nullCharPos As Long

    stringLen = lstrlen(pointerToString)
    buffer = String$(stringLen + 1, vbNullChar)
    ret = lstrcpy(ByVal buffer, ByVal pointerToString)
    nullCharPos = InStr(1, buffer, vbNullChar)
    If nullCharPos > 0 Then buffer = Left(buffer, nullCharPos - 1)
    'Debug.Print buffer
    LPSTRtoVBAstring = buffer
   
End Function

Call it like this from your button click routine:

VBA Code:
    Dim selectedPrinter As String

    selectedPrinter = "\\" & TextBox4.Value & "\" & TextBox16.Value ' or TextBox1.Value

    Delete_All_Printer_Jobs selectedPrinter
@John_w , sorry for the delay , i tested yesterday and it worked . Thank you so much . Best regards
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,958
Members
449,096
Latest member
Anshu121

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top