borderAndreaVB free resources for Visual Basic developersborder

AndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2008 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 10-05-2008 Re: Printer.? by Keithuk
icon 24-09-2007 DocumentProperties and SetPrinter problem by Schimmel
icon 21-07-2006 New to Crystal Report by chintu4u
icon 08-05-2006 Re: printing on roll paper by ozukum
icon 05-03-2006 Re: How can I delete a printer by Goran
:: Sponsored Links



Partners: Il portale per lui e lei | Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

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