Solved

Getting Network/Local Printer List and Setting Default Printer In VB5 & NT

Posted on 1998-07-09
3
250 Views
Last Modified: 2010-05-03
Can you please tell me how to get a local/network Printer list in VB5 under WIN/NT & WIN95 and Set the default printer tto one of those printers?
0
Comment
Question by:moisidis
  • 2
3 Comments
 

Expert Comment

by:MikeABB
Comment Utility
Have you tried the Printers collection?  As long as the printers that you are searching for are installed on the machine, it should be in this collection.  The VB help file gives the following example:

The Printers collection enables you to query the available printers so you can specify a default printer for your application. For example, you may want to find out which of the available printers uses a specific printer driver. The following code searches all available printers to locate the first printer with its page orientation set to portrait, then sets it as the default printer:

Dim X As Printer
For Each X In Printers
      If X.Orientation = vbPRORPortrait Then
            ' Set printer as system default.
            Set Printer = X
            ' Stop looking for a printer.
            Exit For
      End If
Next

You designate one of the printers in the Printers collection as the default printer by using the Set statement. The preceding example designates the printer identified by the object variable X, the default printer for the application.

If this solves your problem, let me know and I'll resubmit this as an answer
0
 
LVL 6

Accepted Solution

by:
clifABB earned 100 total points
Comment Utility
The following code will load a listbox with available printers and when double clicked will set that printer as default.

Place a Listbox on a form.
Add the following code to the form's load event:
Private Sub Form_Load()
  Dim prtTarget As Printer

  For Each prtTarget In Printers
    List1.AddItem prtTarget.DeviceName
  Next prtTarget
End Sub

In the listbox's double click event, place the following code:
Private Sub List1_DblClick()
  Dim prtTarget As Printer
 
  For Each prtTarget In Printers
    If prtTarget.DeviceName = List1.List(List1.ListIndex) Then
      Set Printer = prtTarget
      Exit For
    End If
  Next prtTarget
End Sub
(This code could also be placed in a button's click event)
0
 
LVL 6

Expert Comment

by:clifABB
Comment Utility
By the way, you might want to add the following code to the form's load event after the code in my answer.  It highlights the current default printer:

  Dim nCnt1     As Integer
  For nCnt1 = 0 To List1.ListCount - 1
    If Printer.DeviceName = List1.List(nCnt1) Then
      List1.ListIndex = nCnt1
    End If
  Next nCnt1

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now