Solved

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

Posted on 1998-07-09
3
252 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
ID: 1465342
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
ID: 1465343
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
ID: 1465344
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

810 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