Solved

Using a Common Dialog Box to Print Contents of Various Controls

Posted on 2001-08-24
9
186 Views
Last Modified: 2008-02-01
Hello.

I am trying to print the contents of a RichTextBox using a Windows Common Dialog Box component.

It works to print, if I select OK. If I select Cancel the program crashes with a run-time error #32755.

The name of the common dialog box is 'cmdEd'. It was coded by another developer, without comments, to print the contents of the rtfDisplay RichTextBox control.

The code is as follows:

cdgEd.Flags = cdlPDReturnDC + cdlPDNoPageNums
  If rtfDisplay.SelLength = 0 Then
     cdgEd.Flags = cdgEd.Flags + cdlPDAllPages
  Else
     cdgEd.Flags = cdgEd.Flags + cdlPDSelection
  End If
  cdgEd.ShowPrinter
  rtfDisplay.SelPrint cdgEd.hDC

I presently don't understand these 'Flags'. I know how to code the vbOKOnly or vbCancel on a message box.

How does one properly code this dialog component to insure that clicking on cancel truly does cancel and unload the dialog?

JNowlin
0
Comment
Question by:jnowlin
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 6

Expert Comment

by:JonFish85
ID: 6422251
try something like this:

Private Sub Command1_Click()
On Error Goto Problems
 
  CommonDialog.Show
  'Other code here
  Exit Sub
problems:
  If Err.Number = 32755 Then
    Exit Sub
  Else
    MsgBox Err.Description
  End If
End Sub

hope this works!
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6422257
They have set the CancelError to true.  This allows you to determine when a user presses cancel and take any necessary action, e.g,


Sub PrintStuff()
   On Error Goto PrintStuff_EH

   '...


PrintStuff_EH:
   If Err.Number = cdlCancel Then
      'User pressed cancel
   End If
End Sub
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 6422262
Sorry, CancelError property of the common dialog control.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6423594
jnowlin, i would like to ask for which reason do you use a commondialog to print richtrextbox.
If i remember well, it has incorporated that funcionality itself:RichTextBox1.selprint
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6423646
Ups!
Sorry forget my comment.
0
 

Author Comment

by:jnowlin
ID: 6426398
Hi AzraSound,

I changed the CancelError property of this common dialog control to False. When I ran the program, opened a file into the RichTextBox control, 'rtfDisplay', and selected "Cancel" instead of "Print", I got a different run-time error:

Error #32001
"Invalid HDC"

I don't know what HDC means. It must be some sort of relationship between the common dialog control and the print dialog that opens up within Windows2000 that I'm still unclear about.

Does this Cancel button need to be coded to Exit Sub when a user clicks it?

Jim
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 150 total points
ID: 6426437
>>Does this Cancel button need to be coded to Exit Sub when a user clicks it?

Yes, otherwise, you have no idea whether or not to proceed with the print operation.  For example, look at this logic:


'Without having cancel throw an error
Show print dialog
Try to print with the selected printer  <~~ If nothing was selected (e.g., they pressed cancel), you have no printer to print to
When you call:
[RichTextBox.SelPrint dialogSelectedPrinter.hDC]
the selected printer is nothing, thus, no valid hDC (handle to a device context) is passed


'With cancel throwing an error
On Error SkipAllCode
You still have all your code from above, but its bypassed if the user presses cancel, and taken to your error handling routine
SkipAllCode:
   If Err.Number = cdlCancel
      'user pressed cancel, you can do nothing, ask if they are sure they want to cancel the print, etc.
   End If


You should be able to trap otherwise, even with CancelError set to False, by testing to be sure the hDC is not 0 before trying to print with it.  However, using the more popular form of error handling is probably the way to go.
0
 
LVL 2

Expert Comment

by:ramses
ID: 6431402
When dealing with a CommonDialog Control, I always go something like this

With cdgEd
  .CancelError=True  'always usefull
  .Flags = cdlPDReturnDC + cdlPDNoPageNums
  If rtfDisplay.SelLength = 0 Then
    .Flags = .Flags + cdlPDAllPages
  Else
    .Flags = .Flags + cdlPDSelection
 End If
 On Error Resume Next 'just for the dialog
 .ShowPrinter
 If Err = cdlCancel Then Exit Sub 'user pressed cancel
 On Error Goto 0 'disable error handler
 rtfDisplay.SelPrint .hDC
End With


This is a bit like the examples above, except that Error Handling is only enabled for the Commondialog Control when showing the Printbox.

After showing the Printbox and checking if the user presses cancel, ErrorHandling is disabled so that you can see other errors, should they occur.



Just scream 4 more ;)
0
 

Author Comment

by:jnowlin
ID: 6448960
Thanks AzraSound,

I'm posting another question involving the common dialog control, as this project has code on it (not mine), is poorly commented and I have next to nothing for documentation on this control.

Jim
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help in WHSCRIPT 9 47
VBA to copy paste columns form one file to other 20 86
A macro to Count the number of rows across all worksheets 3 84
Advice in Xamarin 21 57
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now