+ Reply to Thread
Results 1 to 5 of 5

Printerobject DeviceContext

  1. #1
    Registered User
    Join Date
    02-20-2005
    Posts
    6

    Printerobject DeviceContext

    Hi there,

    I'm looking for a way to determine the handle of the actuall printer. Printer.hdc does not work in excel/vba, there is no printer object definition. Background: I want to know some printer properties like resolution using the GetDeviceCaps call, but there I have to pass the printer handle.

    Thanks for any advice in advance,

    TopSat

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello TopSat,

    The API call you need to return the printer handle from VBA or VB is below. This code must be placed inside VB or VBA Module.
    _________________________________________________________________

    Public Sub GetPrinterHandle()

    Declare Function OpenPrinter _
    Lib "winspool.drv" _
    Alias "OpenPrinterA" _
    (ByVal pPrinterName As String, phPrinter As Long, pDefault As Any) As Long

    End Sub

    Notes:

    If the function succeeds, the return value is the printer handle otherwise it is 0.

    · pPrinterName
    [in] Pointer to a null-terminated string that specifies the name of the printer or print server.
    Windows NT/2000: If NULL, it indicates the local printer server.

    · phPrinter
    [out] Pointer to a variable that receives a handle to the open printer or print server object.
    Windows 2000: The phPrinter parameter can return an Xcv handle for use with the XcvData function. For more information about XcvData, see the Microsoft Windows 2000 Driver Development Kit.

    · pDefault
    [in] Pointer to a PRINTER_DEFAULTS structure. This value can be NULL.

    _________________________________________________________________

    Hope this helps,
    Leith Ross

  3. #3
    Registered User
    Join Date
    02-20-2005
    Posts
    6
    Hello Leith,

    thank you for this great advice. I assume with this call, I get the right handle.
    But in fact obviously the return values of GetdeviceCaps are wrong. My be it won't work with excel vba, but doing the same with window properties is fine (mydc = GetDC(ihwndCreatedWindow) - Result= GetDeviceCaps(mydc, LOGPIXELSX) )

    I tried this:

    Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" ( _
    ByVal pPrinterName As String, phPrn As Long, pDefault As Any) _
    As Long
    Private Declare Function GetDeviceCaps Lib "GDI32" _
    (ByVal hDC As Long, ByVal nIndex As Long) As Long


    Private Const PHYSICALWIDTH As Long = 110


    Type Defaults
    pDataType As String
    pDevMode As Long
    DesiredAccess As Long
    End Type

    Dim PrinterDefaults As Defaults

    Dim ThePrinter As String
    Dim lhPrinter As Long 'handle of printer
    Dim lReturn As Long

    ThePrinter = "HP LaserJet 4M/M" 'Name is okay, actually found with other sub
    'only for demonstration filled

    lReturn = OpenPrinter(ThePrinter, lhPrinter, PrinterDefaults)

    MsgBox lhPrinter & "<<<lhPrinter"


    Result = GetDeviceCaps(lhPrinter, PHYSICALWIDTH)

    'Result is alway 0

    ------------------

    Do you have an idea why it won't work as intended?

    Thanks,

    TopSat

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello TopSat,

    In the API call:

    Declare Function OpenPrinter _
    Lib "winspool.drv" _
    Alias "OpenPrinterA" _
    (ByVal pPrinterName As String, phPrinter As Long, pDefault As Any) As Long

    You are probably running Windows 2000 or newer. This call uses ASCII string coding. The newer Windows use Unicode. Change "OpenPrinterA" to "OpenPrinterW".

    Should do it,
    Leith Ross

  5. #5
    Registered User
    Join Date
    02-20-2005
    Posts
    6

    CreateDC

    Hi Leith,

    everything works fine, if I find the printer handle with

    Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" _
    (ByVal lpDriverName As String, ByVal lpDeviceName As String, _
    ByVal lpOutput As Long, ByVal lpInitData As Long) As Long

    Thank you again for your support.

    TopSat

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1