borderAndreaVB free resources for Visual Basic developersborder

AndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2013 Andrea Tincani
:: Get the Default Printer

Author  

André Masson

Language  

VB5, VB6

Operating Systems  

Windows 95, 98 and NT
API Declarations

Option Explicit

'MS Windows API Function Prototypes
Private Declare Function GetProfileString Lib "kernel32.dll" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long

Module

'---------------------------------------------------------------
' 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

Usage

'Example:
'- Start a new project and on the form_load event write this code

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 Sub

:: Navigation

Home

Printing Using Visual Basic

Previous Tip

Next Tip

:: Search this site
Google
:: Related Topics
icon 15-11-2011 Re: PDF by karl93
icon 26-03-2010 Create a DLL from VB Code AS by alanc007
icon 09-02-2009 Re: Print the Contents of a Picture Box by BadaBandar
icon 10-05-2008 Re: Printer.? by Keithuk
icon 24-09-2007 DocumentProperties and SetPrinter problem by Schimmel
:: Sponsored Links



borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2013 Andrea Tincaniborder