tags:

views:

856

answers:

4

I have a VB6 application that is giving an error 5, "Invalid procedure call or argument" when the program attempts to set the Printer object to a specific printer from the Printers collection. The printer in question is some sort of copier/printer running through a print server. The error doesn't occur when setting the Printer object to other printers defined in the collection. Any ideas what might be causing the error 5 in this circumstance? I'm not sure what exactly happens when using the "Set Printer = x" statement in VB6 - is it attempting to interface with the actual printer driver at that point? Is it possible that the driver isn't recognized as a valid printer by the VB6 Printer object for some reason, resulting in the "invalid argument" error?

+1  A: 

The "Invalid procedure call or argument" is for a VB runtime error 5.

I suspect that the error 5 you're seeing is the Win32 error code, which means "Access is denied".

Apparently VB runtime errors differ from Win32 errors - I suspect that it has to do with the roots of VB predating even MS-DOS: http://blogs.msdn.com/ericlippert/archive/2004/09/09/227461.aspx. I'm not sure how you're supposed to determine which interpretation to use when

Michael Burr
Ahh... never thought of that. The printer in question is on a print server, but user can print to it with other apps ok. What could cause VB6 to get "Access denied" when trying to "Set Printer = x"? Does setting the Printer object require having some config rights to the target printer or something?
E Brown
If the user can access the printer outside the program, I'm less inclined to think it might be an access denied problem.
Michael Burr
The error 5 is almost certainly the VB runtime error 5 "Invalid procedure call or argument". You wouldn't get a Win32 error from executing a `Set` statement. You only get Win32 errors from VB6 when making API calls using the `Declare` statement, and then you have to detect them by looking at the `Err.LastDllError` property
MarkJ
+2  A: 

You are using code like this to set it correct? Not just trying to set it by a string?

   Dim strDeviceName As String
   Dim prnCurrent    As Printer

   For Each prnCurrent In Printers

      If UCase$(prnCurrent.DeviceName) = strDeviceName Then

         Set Printer = prnCurrent

         Exit For

      End If

   Next prnCurrent

In order to stop changing the default printer you run this code before you set the printer. Then you won't have to set the default printer back. This also makes your printer selection unique to your program which is what most people want.

' deassociate printer object from default system printer
Printer.TrackDefault = False
Will Rickards
Yes, the app sets the Printer object to a Printer object from the Printers collection. It sets Printer to a receipt printer, prints the receipt, then sets Printer back to the Win default printer. If that is the copier, the error occurs. If the default printer is not the copier, no error occurs.
E Brown
see my revised answer - using Printer.TrackDefault = False to avoid changing the default printer and thus avoid having to set it back.
Will Rickards
A: 

You may want to take a look at the following page:

http://support.microsoft.com/kb/322710

Whenever printing in VB6 I always use this dialog box instead of the common dialog box that comes with VB6. It is a lot more reliable.

Keith Maurino
A: 

I solved this problem in MS Access 2007 VBA by including the server name along with the printer name.

Application.Printer = Application.Printers("\servername\printername")

instead of

Application.Printer = Application.Printers("printername")

Hope this helps someone else.

Paul