Retreive the VB Object "printer" Corresponding to the Default Printer

Paste the following code into it's own BAS module (or convert it to a class if required)

'MS Windows API Function Prototypes
private Declare Function GetProfileString Lib "kernel32" Alias _
     "GetProfileStringA" (byval lpAppName as string, _
     byval lpKeyName as string, byval lpDefault as string, _
     byval lpReturnedString as string, _
     byval nSize as Long) as Long

'---------------------------------------------------------------
' Retreive the vb object "printer" corresponding to the window's
' default printer.
'---------------------------------------------------------------
public Function GetDefaultPrinter() as Printer
    Dim strBuffer as string * 254
    Dim iRetValue as Long
    Dim strDefaultPrinterInfo as string
    Dim tblDefaultPrinterInfo() as string
    Dim objPrinter as Printer

    ' Retreive current default printer information
    iRetValue = GetProfileString("windows", "device", ",,,", _
                strBuffer, 254)
    strDefaultPrinterInfo = Left(strBuffer, InStr(strBuffer, Chr(0)) - 1)
    tblDefaultPrinterInfo = Split(strDefaultPrinterInfo, ",")
    for Each objPrinter In Printers
        If objPrinter.DeviceName = tblDefaultPrinterInfo(0) then
            ' Default printer found !
            Exit for
        End If
    next

    ' If not found, return nothing
    If objPrinter.DeviceName <> tblDefaultPrinterInfo(0) then
        set objPrinter = nothing
    End If

    set GetDefaultPrinter = objPrinter
End Function


Now to test the routine, place the following code into a Form_Load routine :


private Sub Form_Load()
    Dim objPrinter as Printer

    set objPrinter = GetDefaultPrinter()
    MsgBox "Default printer is: " + objPrinter.DeviceName
    MsgBox "Driver name is: " + objPrinter.DriverName
    MsgBox "Port is: " + objPrinter.Port

    set objPrinter = nothing
    End
End Sub




Comments

  • Printer status

    Posted by Legacy on 08/19/2003 12:00am

    Originally posted by: MSCHMITT

    Hi
    How can i get the network printer status under VB ?
    All possible problems with it (Out of order,Need paper...).

    Thanks

    Reply
  • How to change the default printer

    Posted by Legacy on 05/20/2003 12:00am

    Originally posted by: K.K.Birnale

    Hi
    Getting the default printer is OK.But when already a printer is installed and working as a default to the machine,in that case how to change the default printer through coding

    If there is any solution i may be let to the same please
    K.K.Birnale

    Reply
  • Printer Type

    Posted by Legacy on 01/22/2003 12:00am

    Originally posted by: PBS

    Is it posible to determine the default printer type
    e.g.
    is the printer a laser or Dot matrix printer

    Reply
  • ?? Is it possible to find the number of pages printed in a printer

    Posted by Legacy on 12/13/2002 12:00am

    Originally posted by: D. Ramesh babu

    if any time is given, is it possible to find out the number of pages printed by the printer ?

    Reply
  • Thanks

    Posted by Legacy on 07/24/2002 12:00am

    Originally posted by: chew

    This code worked just fine. Might want to add a little bit of error checking though if object is Null when returned. Otherwise, fantastic.

    Reply
  • HOW TO CHANGE THE DEFAULT PRINTER?

    Posted by Legacy on 07/04/2002 12:00am

    Originally posted by: T.A.VENKAT

    Hi

    Getting the default printer is OK.But when already a printer is installed and working as a default to the machine,in that case how to change the default printer through coding

    If there is any solution i may be let to the same please

    t.a.venkat

    Reply
  • Retreive the vb object "printer" corresponding to the Default Printer

    Posted by Legacy on 04/16/2002 12:00am

    Originally posted by: Evans Okosodo

    This code woked! Thanks to the donor and earthweb.com

    Reply
  • Default printer

    Posted by Legacy on 01/05/2002 12:00am

    Originally posted by: Ravi

    This example much easier to get the default printer.

    Reply
  • Capture Printer Queue from Vb

    Posted by Legacy on 11/21/2001 12:00am

    Originally posted by: Samanatha Wijeratne

    capture printer data Queue from VB . and get them into
    data file.

    Reply
  • Good One

    Posted by Legacy on 11/07/2001 12:00am

    Originally posted by: Suresh P

    Hi masson,
    this is good one. congrats
    I have a doubt. I have sent a page to printer. How will i know the print status of the page ?
    (where the page is lying now ? is it in the print queue of the printer or page is printing or print job is over...... )


    Suresh

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • Live Event Date: November 13, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT APIs can be a great source of competitive advantage. The practice of exposing backend services as APIs has become pervasive, however their use varies widely across companies and industries. Some companies leverage APIs to create internal, operational and development efficiencies, while others use them to drive ancillary revenue channels. Many companies successfully support both public and private programs from the same API by varying levels …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds