How can I get a DeviceName of a Printer using PrintDialog function?


How can I get a DeviceName of a Printer using API function (PrintDialog). I tried to get it from a DevMode structure, but the length of a DeviceName in DevMode structure is only 32 (it is too small, i must get whole name, more than 32 characters).

Has somebody any idea? Please for help

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

PAQ_ManConnect With a Mentor Commented:
Question Closed, 125 points refunded.
Community Support Moderator
Hi jesiu
This is a way to view the DeviceName of the printer without getting messed up with API

Dim myPrinter as Printer
Set myPrinter = Printer
Debug.Print myPrinter.DeviceName

Browse the printer class for any other information you need about the default printer
jesiuAuthor Commented:
Hi iHadi,
No.. I don't want to use Printer Object.

Some other ideas?
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

 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
    ' If not found, return nothing
    If objPrinter.DeviceName <> tblDefaultPrinterInfo(0) Then
        Set objPrinter = Nothing
    End If
    Set GetDefaultPrinter = objPrinter
End Function

Private Sub Command1_Click()
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

For discovering printer connections, and which is the default printer, you can use the following logic (coded in VBScript but easily changed to VB) for local connections only... use WMI if you need to interrogate the printers connected to remote computers.

' *********************************************
' VBScript to enumerate all connected printers
' and display the port and name of the printer
' and display which is the default printer.
' If using this code fragment with VB 6, set a
' reference to:  Microsoft Scripting Runtime.
' *********************************************

Option Explicit

Dim WshNetwork, WshShell
Dim oPrinters
Dim arrPrn, strPrn
Dim arrPort, strPort
Dim i, strDescr, strMsg

Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oPrinters  = WshNetwork.EnumPrinterConnections

   '-- enumerate all printer connections
   For i = 0 to oPrinters.Count - 1 Step 2
'     Debug.Print  "Port = " & oPrinters.Item(i) & "  name = " & oPrinters.Item(i+1)
      Wscript.Echo "Port = " & oPrinters.Item(i) & "  name = " & oPrinters.Item(i+1)

   '-- Display the printer that is currently set as "default"
   Set WshShell = WScript.CreateObject("WScript.Shell")

   ' Read printer info from registry
   strPrn = WshShell.RegRead("HKCU\Printers\DeviceOld")

   ' Format printer info
   arrPrn   = Split( strPrn, ",", -1, vbTextCompare )
   strDescr = arrPrn(0)
   arrPort  = Split( arrPrn(2), ":", -1, vbTextCompare )
   strPort  = arrPort(0)

   ' Display the result
   strMsg = vbCrLf & "Default printer is " & strDescr & " on " & strPort
   WScript.Echo strMsg

Set oPrinters  = Nothing
Set WshNetwork = Nothing
Set WshShell   = Nothing

jesiuAuthor Commented:

so:) you can't understand my problem. But old problem:) I've found a solution.

I wanted to get a DeviceName FROM hDC USING PRINTDIALOG function. So if the name of a device is longer than 32 characters you must get it from DevNames -> extra. There you can find DeviceName, DeviceDriver and DevicePort.

Thanks for anwers

All Courses

From novice to tech pro — start learning today.