?
Solved

how to use printerproperties api on winxp/win2k

Posted on 2005-03-05
10
Medium Priority
?
383 Views
Last Modified: 2008-01-09
I'm trying to use printerproperties api function from winspool.drv, but it dosen't work on winxp (it shows the about dialog of the printer driver or a basic property window), on win98, win95 or winME works perfectly. I need it because I want to make a custom printer dialog.

the code goes like this:

Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As Any) As Long
Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Private Declare Function PrinterProperties Lib "winspool.drv" (ByVal hwnd As Long, ByVal hPrinter As Long) As Long
Private Sub Form_Load()
    Dim hPrinter As Long
    OpenPrinter Printer.DeviceName, hPrinter, ByVal 0&
    PrinterProperties Me.hwnd, hPrinter
    ClosePrinter hPrinter
End Sub

Is there any work around to this prob?
0
Comment
Question by:SebaBiglia
[X]
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
  • 5
  • 5
10 Comments
 
LVL 22

Expert Comment

by:DarkoLord
ID: 13469865
What about using commondialog to show printer properties?

Darko
0
 

Author Comment

by:SebaBiglia
ID: 13470438
I'm already using it, but I want to customize the printer dialog, to reduce the number of step you need to print. now the user needs to set my program's specifics opcions, then select a printer, and then configure the printer options, I want to put the two first dialogs in one.

0
 
LVL 22

Expert Comment

by:DarkoLord
ID: 13471197
You can set these options directly with OpenPrinter and GetPrinter APIs:

Private Type DEVMODE
    dmDeviceName As String * CCHDEVICENAME
    dmSpecVersion As Integer
    dmDriverVersion As Integer
    dmSize As Integer
    dmDriverExtra As Integer
    dmFields As Long
    dmOrientation As Integer
    dmPaperSize As Integer
    dmPaperLength As Integer
    dmPaperWidth As Integer
    dmScale As Integer
    dmCopies As Integer
    dmDefaultSource As Integer
    dmPrintQuality As Integer
    dmColor As Integer
    dmDuplex As Integer
    dmYResolution As Integer
    dmTTOption As Integer
    dmCollate As Integer
    dmFormName As String * CCHFORMNAME
    dmUnusedPadding As Integer
    dmBitsPerPel As Integer
    dmPelsWidth As Long
    dmPelsHeight As Long
    dmDisplayFlags As Long
    dmDisplayFrequency As Long
End Type

0
Industry Leaders: 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!

 

Author Comment

by:SebaBiglia
ID: 13471615
I think that you aren't getting my point, I need to place a combo box in a dialog with the list of installed printers (done) and a button next to it to open the selected printer properties, that is what I want to do. It's is supposed to be done using the printerproperties API, but I don't know why it dosen't work on Win2k/WinXP, I need a workarround to this specific problem.

I think that there my be a way because a lot of commercial applications has a custom printing dialog an has the combo with the "properties..." button and it works OK on any OS.
0
 
LVL 22

Expert Comment

by:DarkoLord
ID: 13471688
Well why don't you then just enumerate all printers and put them to combobox?
0
 

Author Comment

by:SebaBiglia
ID: 13471787
I've already done it. But I wan't to open the printer properties window of the selected printer in the combobox with the "printerpoperties" API and it dosen't work, it just shows you a usless about form. If you try the same code on win98 it works just fine. That is my problem, I need to know if there is any way to make the code works on win2k/winxp.w
0
 
LVL 22

Accepted Solution

by:
DarkoLord earned 500 total points
ID: 13471976
Private Const DM_OUT_BUFFER = 2
Private Const DM_IN_PROMPT = 4
Private Const DM_IN_BUFFER = 8
Private Const IDOK = 1
Private Const IDCANCEL = 2
Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
Private Const PRINTER_ACCESS_ADMINISTER = &H4
Private Const PRINTER_ACCESS_USE = &H8
Private Const PRINTER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or PRINTER_ACCESS_ADMINISTER Or PRINTER_ACCESS_USE)

Private Type PRINTER_DEFAULTS
    pDatatype As String
    pDevMode As Long
    DesiredAccess As Long
End Type

Private Declare Function ClosePrinter Lib "winspool.drv" (ByVal hPrinter As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal ByteLen As Long)
Private Declare Function DocumentProperties Lib "winspool.drv" Alias "DocumentPropertiesA" (ByVal hwnd As Long, ByVal hPrinter As Long, ByVal pDeviceName As String, ByVal pDevModeOutput As Long, ByVal pDevModeInput As Long, ByVal fMode As Long) As Long
Private 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 Long
Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" (ByVal pPrinterName As String, phPrinter As Long, pDefault As Any) As Long
Private Declare Function SetPrinter Lib "winspool.drv" Alias "SetPrinterA" (ByVal hPrinter As Long, ByVal Level As Long, pPrinter As Any, ByVal Command As Long) As Long

Public Sub DocumentPropertiesDlg(ByVal hwnd As Long, ByVal PrinterName As String)
    Dim r As Long
    Dim hPrinter As Long
    Dim PD As PRINTER_DEFAULTS
    Dim mDM() As Long
    Dim mPI2() As Long
    Dim BytesNeeded As Long
    Dim Drav As Long
    Dim ErrReport As Boolean
   
    ErrReport = True
    PD.DesiredAccess = PRINTER_ALL_ACCESS
    PD.pDatatype = vbNullString
    PD.pDevMode = 0
    r = OpenPrinter(PrinterName, hPrinter, PD)
    If (r = 0) Or (hPrinter = 0) Then
        GoTo Clean
    End If
    r = DocumentProperties(0, hPrinter, PrinterName, 0, 0, 0)
    If r < 0 Then
        GoTo Clean
    End If
    '1 Long = 4 Byte
    ReDim mDM(r / 4)
    r = DocumentProperties(hwnd, hPrinter, PrinterName, VarPtr(mDM(0)), VarPtr(mDM(0)), DM_OUT_BUFFER Or DM_IN_PROMPT)
    If r = IDCANCEL Then
        GoTo Clean
    End If
    GetPrinter hPrinter, 2, 0, 0, BytesNeeded
    If BytesNeeded = 0 Then
        GoTo Clean
    End If
    ReDim mPI2(BytesNeeded / 4)
    r = GetPrinter(hPrinter, 2, mPI2(0), BytesNeeded, Drav)
    If r = 0 Then
        GoTo Clean
    End If
    mPI2(7) = VarPtr(mDM(0))
    r = SetPrinter(hPrinter, 2, mPI2(0), 0)
    If CBool(r) = False Then
        GoTo Clean
    End If
    ErrReport = False
   
Clean:
    If ErrReport And Err.LastDllError <> 0 Then
        MsgBox Err.LastDllError
    End If
    If hPrinter <> 0 Then
        ClosePrinter hPrinter
    End If
End Sub

Private Sub Command1_Click()
   DocumentPropertiesDlg Me.hwnd, Printer.DeviceName
End Sub
0
 

Author Comment

by:SebaBiglia
ID: 13472181
Now we are talking, that's what I was looking for..

Works like a charm, thank's man.
0
 
LVL 22

Expert Comment

by:DarkoLord
ID: 13472205
You're welcome =)

At first I didn't understand what are you trying to do.. :)
0
 

Author Comment

by:SebaBiglia
ID: 13472360
I'm not a naturally english speaker (or writer), so maybe I didn't explain myself well.

I was looking for this tip for about two months. But everywere I look they talk about printerproperties function but none of them ever mention DocumentProperties.

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month14 days, 22 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