VBA runtimerror 5, from LEFT(Activeprinter function help.

Weeble

Board Regular
Joined
Nov 30, 2016
Messages
95
Office Version
  1. 365
I found a code that could solve alot of my problems, but I get 'Run time Error 5 , Invalid procedure or Argument. Anyone that could help me out with the issue? Code is used for when printing lots of sheets, some papers are missing / skipps. This code is suppost to hold VBA until the sheet has been printed.

Code stops at this step.
Code:
OpenPrinter Left(ActivePrinter, InStr(ActivePrinter, " o") - 1), hPrinter, ByVal 0&



Code:
Option Explicit

'' Win32 API declarations


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, pJob As Any, ByVal cdBuf As Long, pcbNeeded As Long, pcReturned As Long) As Long


Public Sub WaitForPrintingToFinish()
    Do
    Loop Until CountPrintJobs = 0
End Sub


Public Function CountPrintJobs() As Long
    Dim hPrinter As Long, lNeeded As Long, lReturned As Long
    Dim lJobCount As Long
    OpenPrinter Left(ActivePrinter, InStr(ActivePrinter, " o") - 1), hPrinter, ByVal 0&
    EnumJobs hPrinter, 0, 99, 1, ByVal 0&, 0, lNeeded, lReturned
    If lNeeded > 0 Then
        ReDim byteJobsBuffer(lNeeded - 1) As Byte
        EnumJobs hPrinter, 0, 99, 1, byteJobsBuffer(0), lNeeded, lNeeded, lReturned
        If lReturned > 0 Then
            lJobCount = lReturned
        Else
            lJobCount = 0
        End If
    Else
        lJobCount = 0
    End If
    ClosePrinter hPrinter
    CountPrintJobs = lJobCount
End Function

Any help would be awesome, and let me know if you need more info.
I call WaitforPrintingToFinish in my original code.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
I have things a bit different. U can trial this...
Code:
Private Declare Function OpenPrinter Lib "winspool.drv" _
Alias "OpenPrinterA" (ByVal pPrinterName As String, _
phPrinter As Long, pDefault As PRINTER_DEFAULTS) As Long
Public NameOfPrinter As String
Code:
Private Function CheckPrinter(PrinterName) As String
Dim hPrinter As Long
Dim result As Long
Dim pDefaults As PRINTER_DEFAULTS
'Set desired access security setting.
pDefaults.DesiredAccess = PRINTER_ACCESS_USE
'Call API to get a handle to the printer.
result = OpenPrinter(PrinterName, hPrinter, pDefaults)
If result = 0 Then
'If an error occurred, display an error and exit sub.
CheckPrinter = "Cannot open printer " & PrinterName & _
", Error: " & Err.LastDllError
Exit Function
End If
CheckPrinter = "Printer info retrieved"
End Function
To operate...
Code:
Dim temp As Variant
temp = Split(Application.ActivePrinter, "on")
NameOfPrinter = Left(temp(0), Len(temp(0)) - 1)
'check for communication with printer
If CheckPrinter(NameOfPrinter) = "Printer info retrieved" Then
'whatever code
This is a condensed version of the whole which seems like it would work. I think I posted code somewhere. I'll see if I can find a link. HTH. Dave
edit: Here's the link http://www.vbaexpress.com/forum/sho...inter-is-not-connected-quot&highlight=printer
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,899
Messages
6,181,629
Members
453,058
Latest member
rmd0725

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