Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

How to display Advanced document property of a selected printer ?

Total Hit ( 2614)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


This demo will show you how to use AdvancedDocumentProperties function to displays a printer-configuration dialog box for the specified printer, allowing the user to configure that printer.

Step-By-Step Example

- Create a standard exe project
- Place following code in form1 code window
Note: Make sure that you have atleast one default printer setup

Click here to copy the following block
Private Declare Function OpenPrinter Lib "winspool.drv" Alias "OpenPrinterA" ( _
    ByVal pPrinterName As String, _
    phPrinter As Long, _
    pDefault As PRINTER_DEFAULTS) As Long

Private Declare Function AdvancedDocumentProperties Lib "winspool.drv" Alias "AdvancedDocumentPropertiesA" ( _
    ByVal hwnd As Long, _
    ByVal hPrinter As Long, _
    ByVal pDeviceName As String, _
    pDevModeOutput As DEVMODE, _
    pDevModeInput As DEVMODE) As Long

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long

Private Declare Function ClosePrinter Lib "winspool.drv" ( _
    ByVal hPrinter As Long) As Long

Const CCHDEVICENAME = 32
Const CCHFORMNAME = 32

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

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

Private Declare Function PrinterProperties Lib "winspool.drv" (ByVal hwnd As _
    Long, ByVal hPrinter As Long) As Long

Const PRINTER_ACCESS_ADMINISTER = &H4
'///////////////////////////////////////////////////////////////////////////
' Display the Printer Properties dialog box.
' The DeviceName argument is the DeviceName property of the Printer object or
' an item in the Printers collection.
' ParentHWnd is the handle of the window that owns the dialog box.
'
' Returns True if the function succedes.
'///////////////////////////////////////////////////////////////////////////

Public Sub ShowDocumentProperties(sPrinterName As String, _
                  hnd_CallingApplication As Long)
  Dim lAPIResult         As Long  '1 is success , 0 Is failure
  Dim hnd_Printer         As Long  'handle to the Printer
  Dim pd             As PRINTER_DEFAULTS
  Dim dmIN            As DEVMODE
  Dim dmOUT            As DEVMODE
  Dim llngRet As Long

  On Error GoTo ERROR_HANDLER

  '// Set for administrator access and try to open the printer.
  pd.DesiredAccess = PRINTER_ACCESS_ADMINISTER

  '// Try to open as admin.
  If OpenPrinter(sPrinterName, hnd_Printer, pd) = 0 Then
    pd.DesiredAccess = PRINTER_ACCESS_USE Or STANDARD_RIGHTS_REQUIRED
    '// Try to open with printer access
    lAPIResult = OpenPrinter(sPrinterName, hnd_Printer, pd)
  End If

  If hnd_Printer Then
    llngRet = AdvancedDocumentProperties(hnd_CallingApplication, hnd_Printer, _
        sPrinterName, _
        dmIN, _
        dmOUT)

    ClosePrinter (hnd_Printer)
  Else
    MsgBox "Unable to open printer"
  End If

EXIT_POINT:
  Exit Sub
ERROR_HANDLER:
  Err.Raise Number:=Err.Number, _
      source:="cPrinterInfo.ShowPrinterProperties(" _
      & sPrinterName & ")/" & Err.source, _
      Description:=Err.Description
End Sub

Private Sub Form_Load()
  ShowDocumentProperties Printer.DeviceName, FindWindow(vbNullString, Me.Caption)
End Sub

- Press F5 to run the project


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.