GetDefaultDriverName – Retrieving the default printer Driver name

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 LongPrivate Declare Function PrinterProperties Lib "winspool.drv" (ByVal hwnd As _    Long, ByVal hPrinter As Long) As LongPrivate Declare Function SHInvokePrinterCommand Lib "shell32.dll" Alias _    "SHInvokePrinterCommandA" (ByVal hwnd As Long, _    ByVal uAction As enPrinterActions, ByVal Buffer1 As String, _    ByVal Buffer2 As String, ByVal Modal As Long) As LongPrivate Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" _    (ByVal pPrinterName As String, phPrinter As Long, _    pDefault As PRINTER_DEFAULTS) As LongPrivate Declare Function SetPrinter Lib "winspool.drv" Alias "SetPrinterA" _    (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, _    ByVal Command As Long) As LongPrivate Declare Function GetPrinter Lib "winspool.drv" Alias "GetPrinterA" _    (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, _    ByVal cbBuf As Long, pcbNeeded As Long) As LongPrivate Declare Function lstrcpy Lib "kernel32" Alias "lstrcpyA" (ByVal _    lpString1 As String, ByVal lpString2 As Any) As LongPrivate Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As _    Long) As Long' *** Constants for DEVMODE structurePrivate Const CCHDEVICENAME = 32Private Const CCHFORMNAME = 32' *** constants for DesiredAccess member of PRINTER_DEFAULTSPrivate Const STANDARD_RIGHTS_REQUIRED = &HF0000Private Const PRINTER_ACCESS_ADMINISTER = &H4Private Const PRINTER_ACCESS_USE = &H8Private Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _    PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)Private Type DEVMODE   dmDeviceName         As String * CCHDEVICENAME   dmSpecVersion        As Long   dmDriverVersion      As Long   dmSize               As Long   dmDriverExtra        As Long   dmFields             As Long   dmOrientation        As Long   dmPageSize           As Long   dmPaperLength        As Long   dmPaperWidth         As Long   dmScale              As Long   dmCopies             As Long   dmDefaultSource      As Long   dmPrintQuality       As Long   dmColor              As Long   dmDuplex             As Long   dmYResolution        As Long   dmTToption           As Long   dmCollate            As Long   dmFormName           As String * CCHFORMNAME   dmLogPixels          As Long   dmBitsPerPel         As Long   dmPelsWidth          As Long   dmPelsHeight         As Long   dmDisplayFlags       As Long   dmDisplayFrequency   As Long   dmICMMethod          As Long        ' // Windows 95 only   dmICMIntent          As Long        ' // Windows 95 only   dmMediaType          As Long        ' // Windows 95 only   dmDitherType         As Long        ' // Windows 95 only   dmReserved1          As Long        ' // Windows 95 only   dmReserved2          As Long        ' // Windows 95 onlyEnd TypePrivate Type PRINTER_DEFAULTS   pDatatype            As Long   pDevMode             As DEVMODE   DesiredAccess        As LongEnd TypePrivate Enum enPrinterActions   PRINTACTION_OPEN = 0   PRINTACTION_PROPERTIES = 1   PRINTACTION_NETINSTALL = 2   PRINTACTION_NETINSTALLLINK = 3   PRINTACTION_TESTPAGE = 4   PRINTACTION_OPENNETPRN = 5   PRINTACTION_DOCUMENTDEFAULTS = 6   PRINTACTION_SERVERPROPERTIES = 7End Enum' Get the default printer Driver name' Example: Debug.Print GetDefaultDriverName()Private Function GetDefaultDriverName() As String   Dim sTmp As String   On Error Resume Next   sTmp = Space$(1000)   Call GetProfileString("windows", "Device", vbNullString, sTmp, 1000)   sTmp = Replace(sTmp, Chr$(0), vbNullString)   sTmp = Trim$(sTmp)   If sTmp = vbNullString Then      sTmp = Printer.DriverName   Else      sTmp = GetToken(sTmp, ",", 1)   End If   If sTmp = vbNullString Then      sTmp = Printer.DriverName   End If   GetDefaultDriverName = sTmpEnd Function' This function returns the nToken sToken in a string' Example:'   Debug.Print GetToken("This is a test.", " ", 2) ' => "is"Private Function GetToken(sSearchIn As String, sToken As String, _    nToken As Long) As String   Dim nI As Long   Dim nJ As Long   Dim nK As Long   If nToken < 1 Then      GetToken = vbNullString      Exit Function   End If   nK = 0   For nI = 1 To nToken      nJ = nK      nK = InStr(nJ + 1, sSearchIn, sToken)      If nK = 0 Then         If nI = nToken Then            GetToken = Mid$(sSearchIn, nJ + 1, Len(sSearchIn) - nJ)         Else            GetToken = vbNullString         End If         Exit Function      End If   Next   GetToken = Mid$(sSearchIn, nJ + 1, nK - nJ - 1)End Function'========================================' You can find more routines like this on,'  a site devoted to  VB developers'========================================

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS