Determining default printer information

The example in this section uses a Windows API function to return information about the active printer. The information is contained in a single text string. The example parses the string and displays the information in a more readable format.

Private Declare Function GetProfileStringA Lib "kernel32" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As _ String, ByVal nSize As Long) As Long

Sub DefaultPrinterInfo()

Dim strLPT As String * 255 Dim Result As String Call GetProfileStringA _

("Windows", "Device", "", strLPT, 254)

Result = Application.Trim(strLPT) ResultLength = Len(Result)

Comma2 = InStr(Comma1 + 1, Result, ",", 1)

' Gets printer's name

Printer = Left(Result, Comma1 - 1)

' Gets driver

Driver = Mid(Result, Comma1 + 1, Comma2 - Comma1 - 1)

' Gets last part of device line

Port = Right(Result, ResultLength - Comma2)

' Build message

Msg = "Printer:" & Chr(9) & Printer & Chr(13)

Msg = Msg & "Driver:" & Chr(9) & Driver & Chr(13) Msg = Msg & "Port:" & Chr(9) & Port

' Display message

MsgBox Msg, vbInformation, "Default Printer Information" End Sub

Note The ActivePrinter property of the Application object returns the name of the active printer (and lets you change it), but there's no direct way to determine what printer driver or port is being used. That's why this function may be useful.

Figure 11-20 shows a sample message box returned by this procedure.

Figure 11-20: Getting information about the active printer by using a Windows API call.

CD- This example is available on the companion CD-ROM. The filename is % printer ROM info.xlsm.

+1 0

Responses

  • Jung
    How to get the windows 2007 default printer name vba?
    7 years ago

Post a comment