Solved

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

Posted on 1998-07-09
3
255 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

729 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