Article ID: 148723
Article Last Modified on 10/10/2006
-or-
Worksheets("Sheet1").PrintOut
However, when a custom (Microsoft Excel version 5.0 or 7.0) dialog box is
visible on the screen, you receive one of the error messages mentioned in
the "Symptoms" section of this article.
Private Sub CommandButton1_Click()
End Sub
Private Sub CommandButton1_Click()
my_print_macro
End Sub
Sub Show_Form()
UserForm1.Show ' This displays the UserForm.
End Sub
'---------------------
Sub my_print_macro()
Sheets("sheet1").PrintOut ' This prints Sheet1.
End Sub
'-----------------------------------------------------------------------
Option Explicit
Public DoneFlag As Integer, PrintFlag As Integer
Sub MainMacro()
PrintFlag = 0 ' Initialize PrintFlag.
DoneFlag = 0 ' Initialize DoneFlag.
DialogSheets("Dialog1").Show ' Show it initially.
' While the DoneFlag does not equal 1 (which will only occur if the
' DoneButton is clicked), continue to loop through the Sub procedure.
Do
If PrintFlag = 1 Then ' If the PrintFlag is set, then
Worksheets("Sheet1"). PrintOut ' print Sheet1 and
PrintFlag = 0 ' reset the PrintFlag.
DialogSheets("Dialog1").Show ' Reshow it only after
End If ' having called the procedure
Loop Until DoneFlag = 1 ' that hid it.
End Sub
Sub DoneButton_Click()
DoneFlag = 1 ' Set the DoneFlag.
DialogSheets("Dialog1").Hide ' Hide the dialog box.
End Sub
Sub PrintButton_Click()
DoneFlag = 0 ' Ensure DoneFlag set to 0.
PrintFlag = 1 ' Set the PrintFlag.
DialogSheets("Dialog1").Hide ' Hide the dialog box.
End Sub
'-----------------------------------------------------------------------
When you activate the DoneButton or the PrintButton button, the
appropriate Sub procedure (DoneButton_Click or PrintButton_Click) runs.
Within each Sub procedure, the Dialog1 dialog box is hidden and a flag
(DoneFlag or PrintFlag) is set to 1. The MainMacro Sub procedure then
resumes and loops back; if PrintFlag equals 1, the macro prints the
worksheet and redisplays the dialog box; if DoneFlag equals 1, the macro
exits the loop and ends the macro.
'--------------------------------------------------------------------
Sub ShowTheDialog()
' This portion of the macro could be much larger,
' setting variables and conditions prior to displaying the dialog
' box.
' The last line should be this:
DialogSheets("Dialog1").Show
End Sub
Sub PrintTheSheet()
' This macro should be attached to a button with the Dismiss
' property set and should contain only this line:
Application.OnTime Now + TimeValue("00:00:01"), "BackgroundPrint"
End Sub
Sub BackgroundPrint()
' Like ShowTheDialog, this macro can be much larger,
' selecting areas, defining print ranges, or whatever before
' printing.
ActiveSheet.PrintOut
' This line is optional; use it as the last line if you want the
' dialog box to reappear.
Application.OnTime Now + TimeValue("00:00:01"), "RedisplayDialog"
End Sub
Sub RedisplayDialog()
' This optional macro recalls the dialog box without resetting any
' variable, and should contain only this line:
DialogSheets("Dialog1").Show
End Sub
'--------------------------------------------------------------------
'-----------------------------------------------------------------
' Makes the variables case insensitive.
Option Compare Text
' Dimension a Public variable to determine which button is
' clicked in the dialog box.
Dim Flag As String
Sub Show_Dialog()
' Sets Flag = to nothing.
Flag = ""
' Shows Main dialog box.
Do While DialogSheets("Main").Show
' Checks to see which button was clicked.
Select Case Flag
' If clicked "Print," run Print_Macro.
Case "Print"
Print_Macro
' After Print_Macro runs, exit the routine.
' Ignore this line if you want to have
' the Main dialog box pop up again.
Exit Sub
' If clicked "Other," run Other_Macro.
Case "Other"
Other_Macro
' If clicked any other button, exit the macro.
Case Else
Exit Sub
End Select
Loop
End Sub
Sub Set_Flag()
' Sets the variable Flag to the button that calls this macro.
Flag = Application.Caller
End Sub
Sub Print_Macro()
' Print macro goes here.
' This code can be placed under Case "Print" above without
' having this Sub procedure.
MsgBox "Your Print Macro here"
End Sub
Sub Other_Macro()
' Shows secondary dialog.
' This code can be placed under Case "Other" above without
' having this Sub procedure.
DialogSheets("Other").Show
End Sub
'--------------------------------------------------------------------
printout
printpreview
Additional query words: 5.00 5.00a 5.00c 7.00 8.00 xl97user fail fails printing runtime run time chart Run-time Error 1004 PrintOut Method of Sheets Class Failed XL
Keywords: kbdtacode kberrmsg kbfaq kbprb kbprint kbprogramming KB148723