Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 1998-07-09
3
Medium Priority
?
260 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 400 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses

971 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