Results 1 to 3 of 3

Thread: Print Dialog Set Printer Name

  1. #1

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383

    Print Dialog Set Printer Name

    I am printing from a module so I can't use the CommonDialog ocx so I am using the PrintDialog API.
    All I wan't to do is let the user choose the printer to print to.
    I'm not interested in any of the other flags or options.
    The function should only set the name of the new printer chosen.

    I have taken the AllAPI Common Dialogs example and stripped it down to its bare minimum (I think)
    It still looks a bit over the top to me.
    Is there anything else I can remove from this code? (I don't fully understand what is happening)
    Or is there a better way of doing this?
    VB Code:
    1. Option Explicit
    2.  
    3. Const CCHDEVICENAME = 32
    4. Const CCHFORMNAME = 32
    5.  
    6. Private Type PRINTDLG_TYPE
    7.     lStructSize As Long
    8.     hwndOwner As Long
    9.     hDevMode As Long
    10.     hDevNames As Long
    11.     hDC As Long
    12.     flags As Long
    13.     nFromPage As Integer
    14.     nToPage As Integer
    15.     nMinPage As Integer
    16.     nMaxPage As Integer
    17.     nCopies As Integer
    18.     hInstance As Long
    19.     lCustData As Long
    20.     lpfnPrintHook As Long
    21.     lpfnSetupHook As Long
    22.     lpPrintTemplateName As String
    23.     lpSetupTemplateName As String
    24.     hPrintTemplate As Long
    25.     hSetupTemplate As Long
    26. End Type
    27.  
    28. Private Type DEVMODE_TYPE
    29.     dmDeviceName As String * CCHDEVICENAME
    30.     dmSpecVersion As Integer
    31.     dmDriverVersion As Integer
    32.     dmSize As Integer
    33.     dmDriverExtra As Integer
    34.     dmFields As Long
    35.     dmOrientation As Integer
    36.     dmPaperSize As Integer
    37.     dmPaperLength As Integer
    38.     dmPaperWidth As Integer
    39.     dmScale As Integer
    40.     dmCopies As Integer
    41.     dmDefaultSource As Integer
    42.     dmPrintQuality As Integer
    43.     dmColor As Integer
    44.     dmDuplex As Integer
    45.     dmYResolution As Integer
    46.     dmTTOption As Integer
    47.     dmCollate As Integer
    48.     dmFormName As String * CCHFORMNAME
    49.     dmUnusedPadding As Integer
    50.     dmBitsPerPel As Integer
    51.     dmPelsWidth As Long
    52.     dmPelsHeight As Long
    53.     dmDisplayFlags As Long
    54.     dmDisplayFrequency As Long
    55. End Type
    56. Private Declare Function PrintDialog Lib "comdlg32.dll" Alias "PrintDlgA" (pPrintdlg As PRINTDLG_TYPE) As Long
    57. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (hpvDest As Any, hpvSource As Any, ByVal cbCopy As Long)
    58. Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
    59.  
    60.  
    61. Private Sub Form_Load()
    62.     'KPD-Team 1998
    63.     'URL: [url]http://www.allapi.net/[/url]
    64.     'E-Mail: [email][email protected][/email]
    65.  
    66.     Command5.Caption = "ShowPrinter"
    67.  
    68. End Sub
    69. Private Sub Command5_Click()
    70.     ShowPrinter Me
    71. End Sub
    72.  
    73. Public Sub ShowPrinter(frmOwner As Form, Optional PrintFlags As Long)
    74.     '-> Code by Donald Grover
    75.     Dim PrintDlg As PRINTDLG_TYPE
    76.     Dim DevMode As DEVMODE_TYPE
    77.     Dim lpDevMode As Long
    78.     Dim objPrinter As Printer
    79.     Dim NewPrinterName As String
    80.  
    81.     ' Use PrintDialog to get the handle to a memory
    82.     ' block with a DevMode and DevName structures
    83.  
    84.     PrintDlg.lStructSize = Len(PrintDlg)
    85.     PrintDlg.hwndOwner = frmOwner.hWnd
    86.  
    87.     PrintDlg.flags = PrintFlags
    88.     On Error Resume Next
    89.  
    90.     'Call the print dialog up and let the user make changes
    91.     If PrintDialog(PrintDlg) <> 0 Then
    92.  
    93.         'Next get the DevMode structure and set the printer
    94.         'properties appropriately
    95.         lpDevMode = GlobalLock(PrintDlg.hDevMode)
    96.         CopyMemory DevMode, ByVal lpDevMode, Len(DevMode)
    97.  
    98.         NewPrinterName = UCase$(Left(DevMode.dmDeviceName, InStr(DevMode.dmDeviceName, Chr$(0)) - 1))
    99.         If Printer.DeviceName <> NewPrinterName Then
    100.             For Each objPrinter In Printers
    101.                 If UCase$(objPrinter.DeviceName) = NewPrinterName Then
    102.                     Set Printer = objPrinter
    103.                     'set printer toolbar name at this point
    104.                 End If
    105.             Next
    106.         End If
    107.  
    108.         On Error GoTo 0
    109.     End If
    110.     MsgBox NewPrinterName
    111. End Sub

  2. #2
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: Print Dialog Set Printer Name

    You could make a small form with a combo box and fill it with

    VB Code:
    1. Dim p As Printer
    2. For Each p In Printers
    3.   cmbPrinters.Add(p.DeviceName)
    4. Next p

  3. #3

    Thread Starter
    Frenzied Member agmorgan's Avatar
    Join Date
    Dec 2000
    Location
    Lurking
    Posts
    1,383

    Re: Print Dialog Set Printer Name

    It doesn't look right if you do that though.
    Unless you spend ages making the form look like the print dialog.

Posting Permissions

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



Click Here to Expand Forum to Full Width