Print with Commondialog?

Posted on 2000-04-19
Last Modified: 2010-05-02
How can I print using a common dialog form? Here's my code. Can you tell my why it doesn't work?

Private Sub mnuPrint_Click()
Dim BeginPage, EndPage, NumCopies, Orientation
   ' Set Cancel to True.
   CD1.CancelError = True
   On Error GoTo ErrHandler
   ' Display the Print dialog box.
   ' Get user-selected values from the dialog box.
   BeginPage = CD1.FromPage
   EndPage = CD1.ToPage
   NumCopies = CD1.Copies
   Orientation = CD1.Orientation
   Printer.Copies = NumCopies
   Printer.Orientation = Orientation
   Printer.Print Main.Text
   Exit Sub
   ' User pressed Cancel button.
End Sub
Question by:RogueAce

Expert Comment

ID: 2732524

'Place your error handling first.
On Error GoTo ErrHandler

frmMain.CommonDialog1.CancelError = True
frmMain.CommonDialog1.Flags = &H10& Or &H200&
frmMain.CommonDialog1.Action = 5  'Show printer box

'Set your action to CODE number 5

'Your code here looks good
' Get user-selected values from the dialog box.
   BeginPage = CD1.FromPage
   EndPage = CD1.ToPage
   NumCopies = CD1.Copies
   Orientation = CD1.Orientation

   Printer.Copies = NumCopies
   Printer.Orientation = Orientation
'Use a for-next loop for the copies  
For i = 1 To NumCopies
    Printer.Print frmMain.Text1.Text
Next i

'Then your final code looked good also
   Exit Sub
   ' User pressed Cancel button.

Well Done.  I help this helps you get going.  Good Luck!

Accepted Solution

sevenofnine earned 100 total points
ID: 2732530
You may need to get rid of the CD1.Action line(I couldn't get this to work).  You are 99% of the way there - you just need to add Printer.EndDoc after Printer.Print - you had already sent the document to the print queue, it just needed to be "released" as it were.
Hope that helps!

Author Comment

ID: 2749161
Thanks for the info. As 7 of 9 said, there does have to be an EndDoc statement after the printing code for optimal print handling.

Thank you again.

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Change the path of the data source in my Excel pivot table with macro 4 53
message box in access 4 52
Visual Studio 2005 text editor 10 48
Passing a Text Box name to a Sub 6 102
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

756 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