Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Using VBA to control Printer Properties

dpscottdpscott Posts: 1Member
Hello,

I'm using a VB button to print a report, and I want to be able to bring up the print properties before the document actually begins printing. The Microsoft Suggeseted code is this:

[size=1][size=2]1. Start a new project in Visual Basic. Form1 is created by default.
2. Add a CommandButton and a ListBox to the Form.
3. Paste the following code into the Form's module:

[size=2]Option Explicit

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

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 ClosePrinter Lib "winspool.drv" _
(ByVal hPrinter As Long) As Long

Private Type PRINTER_DEFAULTS
pDatatype As Long ' String
pDevMode As Long
pDesiredAccess As Long
End Type

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 Sub Form_Load()
Dim I As Integer

' List all available printers
For I = 0 To Printers.Count - 1
List1.AddItem Printers(I).DeviceName
If Printers(I).DeviceName = Printer.DeviceName Then
List1.Selected(I) = True ' Select current default printer
End If
Next I
End Sub

Private Sub List1_Click()
Dim Prt As Printer

' Find and use the printer just selected in the ListBox
For Each Prt In Printers
If Prt.DeviceName = List1.Text Then
Set Printer = Prt
Exit For
End If
Next
End Sub

Private Sub Command1_Click()
Dim RetVal As Long, hPrinter As Long
Dim PD As PRINTER_DEFAULTS

PD.pDatatype = 0
' Note that you cannot request more rights than you have as a user
PD.pDesiredAccess = STANDARD_RIGHTS_REQUIRED Or PRINTER_ACCESS_USE
PD.pDevMode = 0
RetVal = OpenPrinter(Printer.DeviceName, hPrinter, PD)
If RetVal = 0 Then
MsgBox "OpenPrinter Failed!"
Else
RetVal = PrinterProperties(Me.hwnd, hPrinter)
RetVal = ClosePrinter(hPrinter)
End If
End Sub[/size]


4. Run the project and click on Command1. The Properties dialog will open for the currently selected printer. Note that some tabs may be missing from what you see when bringing up these properties by hand.[/size]

The error I run into with this is the Printer obeject cannot be found. Any suggestions would be greatly appreciated. Thanks

Comments

  • dokken2dokken2 Posts: 532Member

    : The error I run into with this is the Printer obeject cannot be
    : found. Any suggestions would be greatly appreciated. Thanks
    :

    worked fine for me when i placed the code in VB6. which VB are you using? do have a printer installed? the code lists them and selects the default one.
Sign In or Register to comment.