when i press cancel button.it still prints.Kindly let me know .any help would be Highly appreciated.here is the following code what i have written.

Private Sub btPrint_Click() 
Dim frm As IspecialForm 
Set frm = Me.ActiveForm 
frm.GetTextBoxInvisible 
frm.Refresh 
'CommonDialog1.Flags = cdlPDReturnDC + cdlPDNoPageNums 
CommonDialog1.Action = 5 
CommonDialog1.PrinterDefault = True 
CommonDialog1.CancelError = True 
CommonDialog1.Orientation = cdlPortrait 
CommonDialog1.ShowPrinter 
frm.PrintForm 
Printer.EndDoc 
frm.SetStartDesign 
End Sub

Recommended Answers

All 6 Replies

how about adding an if statement like:

if CommonDialog1.CancelError = True  then
exit sub
end if

just a suggestion maybe other guys around here got a better solution :)

Goodluck!

Actually cguan_77 you are not far off...

Private Sub btPrint_Click() 

On Error GoTo btPrint_ClickError

Dim frm As IspecialForm 
Set frm = Me.ActiveForm 
frm.GetTextBoxInvisible 
frm.Refresh 
'CommonDialog1.Flags = cdlPDReturnDC + cdlPDNoPageNums 
CommonDialog1.Action = 5 
CommonDialog1.PrinterDefault = True 
CommonDialog1.CancelError = True 
CommonDialog1.Orientation = cdlPortrait 
CommonDialog1.ShowPrinter 
frm.PrintForm 
Printer.EndDoc 
frm.SetStartDesign 

Exit Sub
btPrint_ClickError:

If Err.Number = 32755 Then Exit Sub 'User Pressed Cancel

MsgBox "btPrint_Click " & Err.Number & ":" & Err.Description

End Sub

The reason it is still printing is because you had no error handler.


Good Luck

Thank you VB5 Programmer.right now it is working .for printer ms word printer dialog box is not possible?.Kindly let me know.Any help would be Highly Appreciated.the following code is working fine.ms word printer dialog box is not possible instead of terrible boredom Notepad Printer dialog box.Kindly let me know.the following code is working fine.
fine.

Private Sub btPrint_Click()
Dim frm As IspecialForm
Set frm = Me.ActiveForm
If frm Is Nothing Then Exit Sub
On Error GoTo btprint_Error
CommonDialog1.CancelError = True         'this will cause the control to generate error when cancel button is clicked
CommonDialog1.ShowPrinter
btprint_Error:
 If Err.Number = 32755 Then
  Printer.KillDoc
  MsgBox "Dialog cancelled"
  Exit Sub
End If
CommonDialog1.PrinterDefault = True
CommonDialog1.Orientation = cdlPortrait
frm.GetTextBoxInvisible
frm.Refresh
frm.PrintForm
Printer.EndDoc
frm.SetStartDesign
End Sub

now i want to call this printer dialog box.when user click at print
button .Kindly let me know the idea.how should i call this print
dialog box .using print button.Kindly help me.any help would be
Greatly appreciated.here is the following code.i want to call this
dll function at print buttton.Kindly find the attachment also.

Private Declare Function PrintDlg Lib "COMDLG32.DLL" Alias "PrintDlgA" (prtdlg As TPRINTDLG) As Integer

Public Enum EPrintDialog
    PD_ALLPAGES = &H0
    PD_SELECTION = &H1
    PD_PAGENUMS = &H2
    PD_NOSELECTION = &H4
    PD_NOPAGENUMS = &H8
    PD_COLLATE = &H10
    PD_PRINTTOFILE = &H20
    PD_PRINTSETUP = &H40
    PD_NOWARNING = &H80
    PD_RETURNDC = &H100
    PD_RETURNIC = &H200
    PD_RETURNDEFAULT = &H400
    PD_SHOWHELP = &H800
    PD_ENABLEPRINTHOOK = &H1000
    PD_ENABLESETUPHOOK = &H2000
    PD_ENABLEPRINTTEMPLATE = &H4000
    PD_ENABLESETUPTEMPLATE = &H8000
    PD_ENABLEPRINTTEMPLATEHANDLE = &H10000
    PD_ENABLESETUPTEMPLATEHANDLE = &H20000
    PD_USEDEVMODECOPIES = &H40000
    PD_USEDEVMODECOPIESANDCOLLATE = &H40000
    PD_DISABLEPRINTTOFILE = &H80000
    PD_HIDEPRINTTOFILE = &H100000
    PD_NONETWORKBUTTON = &H200000
End Enum

Private Type DEVNAMES
    wDriverOffset As Integer
    wDeviceOffset As Integer
    wOutputOffset As Integer
    wDefault As Integer
End Type

Private Const CCHDEVICENAME = 32
Private Const CCHFORMNAME = 32

Private Type DevMode
    dmDeviceName As String * CCHDEVICENAME
    dmSpecVersion As Integer
    dmDriverVersion As Integer
    dmSize As Integer
    dmDriverExtra As Integer
    dmFields As Long
    dmOrientation As Integer
    dmPaperSize As Integer
    dmPaperLength As Integer
    dmPaperWidth As Integer
    dmScale As Integer
    dmCopies As Integer
    dmDefaultSource As Integer
    dmPrintQuality As Integer
    dmColor As Integer
    dmDuplex As Integer
    dmYResolution As Integer
    dmTTOption As Integer
    dmCollate As Integer
    dmFormName As String * CCHFORMNAME
    dmUnusedPadding As Integer
    dmBitsPerPel As Integer
    dmPelsWidth As Long
    dmPelsHeight As Long
    dmDisplayFlags As Long
    dmDisplayFrequency As Long
End Type

' New Win95 Page Setup dialogs are up to you
Private Type POINTL
    X As Long
    Y As Long
End Type

Private Type RECT
    Left As Long
    TOp As Long
    Right As Long
    Bottom As Long
End Type

Private Type TPAGESETUPDLG
    lStructSize                 As Long
    hWndOwner                   As Long
    hDevMode                    As Long
    hDevNames                   As Long
    flags                       As Long
    ptPaperSize                 As POINTL
    rtMinMargin                 As RECT
    rtMargin                    As RECT
    hInstance                   As Long
    lCustData                   As Long
    lpfnPageSetupHook           As Long
    lpfnPagePaintHook           As Long
    lpPageSetupTemplateName     As Long
    hPageSetupTemplate          As Long
End Type

' EPaperSize constants same as vbPRPS constants
Public Enum EPaperSize
    epsLetter = 1          ' Letter, 8 1/2 x 11 in.
    epsLetterSmall         ' Letter Small, 8 1/2 x 11 in.
    epsTabloid             ' Tabloid, 11 x 17 in.
    epsLedger              ' Ledger, 17 x 11 in.
    epsLegal               ' Legal, 8 1/2 x 14 in.
    epsStatement           ' Statement, 5 1/2 x 8 1/2 in.
    epsExecutive           ' Executive, 7 1/2 x 10 1/2 in.
    epsA3                  ' A3, 297 x 420 mm
    epsA4                  ' A4, 210 x 297 mm
    epsA4Small             ' A4 Small, 210 x 297 mm
    epsA5                  ' A5, 148 x 210 mm
    epsB4                  ' B4, 250 x 354 mm
    epsB5                  ' B5, 182 x 257 mm
    epsFolio               ' Folio, 8 1/2 x 13 in.
    epsQuarto              ' Quarto, 215 x 275 mm
    eps10x14               ' 10 x 14 in.
    eps11x17               ' 11 x 17 in.
    epsNote                ' Note, 8 1/2 x 11 in.
    epsEnv9                ' Envelope #9, 3 7/8 x 8 7/8 in.
    epsEnv10               ' Envelope #10, 4 1/8 x 9 1/2 in.
    epsEnv11               ' Envelope #11, 4 1/2 x 10 3/8 in.
    epsEnv12               ' Envelope #12, 4 1/2 x 11 in.
    epsEnv14               ' Envelope #14, 5 x 11 1/2 in.
    epsCSheet              ' C size sheet
    epsDSheet              ' D size sheet
    epsESheet              ' E size sheet
    epsEnvDL               ' Envelope DL, 110 x 220 mm
    epsEnvC3               ' Envelope C3, 324 x 458 mm
    epsEnvC4               ' Envelope C4, 229 x 324 mm
    epsEnvC5               ' Envelope C5, 162 x 229 mm
    epsEnvC6               ' Envelope C6, 114 x 162 mm
    epsEnvC65              ' Envelope C65, 114 x 229 mm
    epsEnvB4               ' Envelope B4, 250 x 353 mm
    epsEnvB5               ' Envelope B5, 176 x 250 mm
    epsEnvB6               ' Envelope B6, 176 x 125 mm
    epsEnvItaly            ' Envelope, 110 x 230 mm
    epsenvmonarch          ' Envelope Monarch, 3 7/8 x 7 1/2 in.
    epsEnvPersonal         ' Envelope, 3 5/8 x 6 1/2 in.
    epsFanfoldUS           ' U.S. Standard Fanfold, 14 7/8 x 11 in.
    epsFanfoldStdGerman    ' German Standard Fanfold, 8 1/2 x 12 in.
    epsFanfoldLglGerman    ' German Legal Fanfold, 8 1/2 x 13 in.
    epsUser = 256          ' User-defined
End Enum

' EPrintQuality constants same as vbPRPQ constants
Public Enum EPrintQuality
    epqDraft = -1
    epqLow = -2
    epqMedium = -3
    epqHigh = -4
End Enum

Public Enum EOrientation
    eoPortrait = 1
    eoLandscape
End Enum

Private Sub btPrint_Click()

On Error GoTo On_ClickError

Dim frm As IspecialForm
Set frm = Me.ActiveForm
frm.GetTextBoxInvisible
frm.Refresh
'CommonDialog1.Flags = cdlPDReturnDC + cdlPDNoPageNums
CommonDialog1.Action = 5
CommonDialog1.PrinterDefault = True
CommonDialog1.CancelError = True
CommonDialog1.Orientation = cdlPortrait
CommonDialog1.ShowPrinter
frm.PrintForm
Printer.EndDoc
frm.SetStartDesign

Exit Sub
On_ClickError:

If Err.Number = 32755 Then Exit Sub 'User Pressed Cancel

MsgBox "btPrint_Click " & Err.Number & ":" & Err.Description

MsgBox “Printing Stopped”

End Sub

================================================

when i press cancel button.it still prints.Kindly let me know .any help would be Highly appreciated.here is the following code what i have written.

Private Sub btPrint_Click() 
Dim frm As IspecialForm 
Set frm = Me.ActiveForm 
frm.GetTextBoxInvisible 
frm.Refresh 
'CommonDialog1.Flags = cdlPDReturnDC + cdlPDNoPageNums 
CommonDialog1.Action = 5 
CommonDialog1.PrinterDefault = True 
CommonDialog1.CancelError = True 
CommonDialog1.Orientation = cdlPortrait 
CommonDialog1.ShowPrinter 
frm.PrintForm 
Printer.EndDoc 
frm.SetStartDesign 
End Sub

i want to call Private Declare Function PrintDlg Lib "COMDLG32.DLL"
instead of print dialog box.Kindly let me know .any help would be
highly appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.