Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB6 Print Problem - keeps printing to default printer?

Status
Not open for further replies.
Nov 18, 2008
5
I am trying to code a vb app written in VB6 to print to a specific printer. I wrote a pretty simple version of the app just to test this functionality. The app is run from a Win2K3 server and tries to print the text "123" to the network printer \\print_server\ITSystems1.

The probelem I have is that, when I run the app it prints to whatever the default printer is set to on the server and not to where I want it to go. (\\print_server\ITSystems1)

Any thoughts?


Private Sub main()

printer_selected = "\\print_server\ITSystems1"
Printer.Print "123"

Call setPrinter

End Sub
Private Sub setPrinter()

For Each p In Printers
If prefixEqualsIgnoreCase(p.DeviceName, printer_selected) Then
Set Printer = p
Else
End If
Next
End Sub

Public Function prefixEqualsIgnoreCase(s1 As String, s2 As String) As Boolean
prefixEqualsIgnoreCase = True
If UCase(Left(s1, Len(s2))) <> UCase(s2) Then prefixEqualsIgnoreCase = False
End Function


Thanks
 
One thing I noticed is that you have no exit in the for loop. Because of this, SetPrinter could be overwritten. In addition, your else section is redundant as nothing is happening.

Try this:
Code:
Private Sub setPrinter()

For Each p In Printers
   If prefixEqualsIgnoreCase(p.DeviceName, printer_selected) Then
     Set Printer = p
     exit For
   End If
Next
End Sub
 
>Because of this, SetPrinter could be overwritten

Could you explain how?
 
Here's an example.

Say you have printers a, b, c.

Starting the for loop, printer 'a' will be checked.
Outcome: set_printer = 'a'

Say a = "\\print_server\ITSystems1"

You have no way to exit the loop. Therefore, the loop continues to the next value.
Outcome: set_printer = 'b'.
Set_printer 'b' has now be overwritten with value 'b'.

Since there are still more values for the for loop to verify, the for loop is executed a third time.
Outcome: set_printer = 'c'

Say 'c' = Windows default printer.

Now the loop is completed and Set_printer has been overwritten with the value 'c' instead of returning the value 'a'.


You could also try something like:
(very similar to your procedure)
Code:
Dim prn As Printer

For Each prn In Printers
  If prn.DeviceName Like "*" & Trim$(printer_selected) & "*" Then
     Set Printer = prn
     Exit For
  End If
Next

I hope this is a bit clearer for you.
 
I'm afraid it was a leading question. I suspected you thought that. And I am afraid you are in error, since there is a condition being checked. The set only occurs if p.DeviceName and printer_selected match

And you can't have two devices with the same name, so you'll only ever get one match (at best). So you can't overwrite it with the wrong value
 
\\print_server\ITSystems1 must be an installed printer otherwise it does not show up in the printer collection to get set as the default.



David Paulson

 
Guys, thanks for all your help. I'm afraid we were completely over thinking the problem. Dave was right, we didn't have the printers that we needed installed under the profile that was running the app.

We're running this as a scheduled task from the server under a domain "X" account. I was logged into the server as myself and testing it. I have the printers installed under my profile but I needed to log in as under the "X" account and setup the printers under that profile.

Thanks again for everyone's help.
 
I'm glad it's working for you, but it would still print to the default printer because Printer.Print "123" is before you actually set the new default printer to \\print_server\ITSystems1 so upon the first run of this code, it will print to the original default printer and on subsequent runs it would print to \\print_server\ITSystems1 because you did not reset the printer back to the original default. So good practice would be to
1. Get name of the default printer
2. Set printer to new default
3. Print
4. set printer back to original printer
5. exit program



David Paulson

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top