Link to home
Start Free TrialLog in
Avatar of jnowlin
jnowlin

asked on

Using a Common Dialog Box to Print Contents of Various Controls

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
Avatar of JonFish85
JonFish85

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!
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
Sorry, CancelError property of the common dialog control.
Avatar of Richie_Simonetti
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
Ups!
Sorry forget my comment.
Avatar of jnowlin

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of AzraSound
AzraSound
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 ;)
Avatar of jnowlin

ASKER

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