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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing which printer to print to ?

Status
Not open for further replies.

sborny

Technical User
Jun 29, 2001
157
0
0
GB
Hi all,

I have searched and found lots of articles about changing the default printer and they all seem to be based on using the printers name. I am writing an app that has no GUI and runs over night that pulls records from a database and based on the criteria in a particular field prints to different printers. There will be 3 different printers required but they will not always have the same names so I cannot hard code the printer names into the app as it would not work on all the machines.

Does anyone have any idea how I can change the default printer without using the printer names.

Hope this makes sense.

Cheers.

Everything has an answer, it's just knowing the right question to ask. !!!!
 
How will you decide which printer to print on without knowing what they are?

You can find the available printer names like this:

For a = 0 to Printers.Count - 1
Debug.Print Printers(a).Devicename
Next a

You could use the Instr function to find "Laser" for instance

If Instr(1,printers(a).devicename,"Laser") Then


To set the default printer once you decide the name:

For Each prtDev In Printers
If prtDev.DeviceName = "DMP" Then
Set Printer = prtDev
End If
Next

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

For tsunami relief donations

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
Printers are normally accessed by their names. But you don't need to hard-code printer name in your application. You can ask the user to specify the printer name for each application.

Or otherwise, you can retrieve the printer names programatically and query each printer to determine its capabilities and subsequently select correct printer for each application.

You can also mention the appropriate printer in a configuration file or in registry and let the program select the correct printer based on that info at startup.

You can use the Windows scripting host's Network object to retrieve printer names.
___
[tt]
Private Sub Form_Load()
AutoRedraw = True
Dim Printer
For Each Printer In CreateObject("WScript.Network").EnumPrinterConnections
Print Printer
Next
End Sub[/tt]
 
Oops! of course, you can retrieve printer names using VB itself, as shown by johnwm.

(I was thinking in the context of VBA where Printers collection is not available.)
 
Thanks guys,

i will have a look at your code Johnwm and see if I can get things to work that way.

Thanks again.

Everything has an answer, it's just knowing the right question to ask. !!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top