3

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?

E Brown
  • 692
  • 1
  • 8
  • 18
  • Are you sure the error is on the Set Printer line? I've encountered errors with setting different properties which didn't apply to a specific printer. But not on the set line. – Will Rickards Apr 10 '09 at 14:31
  • Yes, it is right on the "Set Printer" line. The source is line-numbered, and the error handler displays the Erl value as part of the error message. – E Brown Apr 10 '09 at 14:43

5 Answers5

3

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
  • 333,147
  • 50
  • 533
  • 760
  • 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 Apr 10 '09 at 15:06
  • 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 Apr 10 '09 at 15:51
  • 1
    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 Aug 16 '10 at 16:19
2

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
  • 2,776
  • 2
  • 19
  • 25
  • 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 Apr 10 '09 at 14:56
  • see my revised answer - using Printer.TrackDefault = False to avoid changing the default printer and thus avoid having to set it back. – Will Rickards Apr 10 '09 at 15:34
1

I was getting this error and after a couple of hours of frustration found my answer. It was a case sensitive issue. I had a Capital "S" when I should have had a lower case "s".

So when the code looked like this:

XeroxReport = "\\Share\Red Xerox 430"
Set Application.Printer = Application.Printers(XeroxReport)

I would get error 5

But when I changed it to:

XeroxReport = "\\share\Red Xerox 430"
Set Application.Printer = Application.Printers(XeroxReport)

presto it worked

aludvigsen
  • 5,893
  • 3
  • 26
  • 37
Tbair8
  • 11
  • 1
1

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.

stealthyninja
  • 10,343
  • 11
  • 51
  • 59
Paul
  • 11
  • 1
0

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
  • 3,374
  • 10
  • 40
  • 48