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?
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
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
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.
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.