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

Posted on 2005-03-22
Medium Priority
Last Modified: 2008-02-26

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

Question by:jesiu
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 13

Expert Comment

ID: 13604625
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

Author Comment

ID: 13604826
Hi iHadi,
No.. I don't want to use Printer Object.

Some other ideas?
LVL 19

Expert Comment

ID: 13605663
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 22

Expert Comment

ID: 13606015
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


Author Comment

ID: 13621126

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


Accepted Solution

PAQ_Man earned 0 total points
ID: 13648313
Question Closed, 125 points refunded.
Community Support Moderator

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question