Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
To work around this problem, use any of the following methods.
Method 1
Use a UserForm instead of a custom (Microsoft Excel version 5.0 or 7.0)
dialog sheet. For example, follow these steps:
- In a new workbook type hello in
cell A1 of Sheet1, and then start the Visual Basic Editor.
- On the Insert menu, click UserForm.
- Add a command button to the UserForm.
- On the View menu, click Code to display the module
sheet for the UserForm.
On the UserForm1 (Code) module, you see the following code displayed:
Private Sub CommandButton1_Click()
End Sub
- Modify this code to read:
Private Sub CommandButton1_Click()
my_print_macro
End Sub
- On the Insert menu, click Module.
This will insert a basic module sheet into your project.
- On this module sheet, type the following code:
Sub Show_Form()
UserForm1.Show ' This displays the UserForm.
End Sub
Sub my_print_macro()
Sheets("sheet1").PrintOut ' This prints Sheet1.
End Sub
- Run the Show_Form macro.
The UserForm you created appears.
- Click the command button on the UserForm.
Sheet1 of your workbook prints.
- Dismiss the UserForm.
Method 2
You can use a method called tunneling to remove the custom (Microsoft
Excel version 5.0 or 7.0) dialog box, perform your print action, and then
redisplay the dialog box.
To use the
PrintOut method, you must first hide or dismiss all custom (Microsoft Excel version 5.0 or 7.0) dialog boxes.
The following Visual Basic code example uses tunneling to display a custom
(Microsoft Excel version 5.0 or 7.0) dialog box, to hide the dialog box
before it prints a worksheet, and to then redisplay the dialog box when
the print operation is complete.
This example assumes that you have a (Microsoft Excel version 5.0 or
7.0) dialog sheet called "Dialog1" (without the quotation marks) and a
worksheet called "Sheet1" (without the quotation marks) that are located
in the same workbook, and that you have a macro module with the following
macro. The dialog sheet must contain two buttons: DoneButton and
PrintButton.
Before you run the macro, you need to assign the appropriate macros to
the DoneButton and the PrintButton. To do this, follow these steps:
- Activate the dialog sheet.
- Select the DoneButton.
- Right-click the DoneButton, and then click Assign Macro on the shortcut menu.
- In the Assign Macro dialog box, click the DoneButton_Click macro, and then click OK.
To assign the PrintButton_Click macro to the PrintButton, repeat steps 1
through 4 above and substitute PrintButton for DoneButton and
PrintButton_Click for DoneButton_Click.
To run the following example, position the insertion point in the line
that reads "Sub MainMacro()," and then press F5.
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.
In this way, the
PrintOut method is only executed if the Dialog1 dialog box is not visible on the screen, and the dialog box will reappear until you exit the loop by activating the DoneButton.
Method 3
This method provides another way to work around the behavior. Use the
OnTime method to allow the macro that contains the
DialogSheets("sheetname").Show to complete before you run the
PrintOut method.
The following sample procedures illustrate this workaround. When you use
this code, the print button dismisses the dialog box, and one second
later, the print macro runs. One second after the sheet prints, the dialog
box appears again.
Note the following regarding the sample procedures:
- Sub ShowTheDialog can be much longer, but the .Show
line should be the last line of the Sub procedure.
- Sub PrintTheSheet is a macro attached to a button that has
been set to have the Dismiss property.
- Sub BackgroundPrint contains the actual PrintOut
method.
- Sub ShowTheDialog is an optional macro to bring the dialog box back.
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
Method 4
This workaround checks to see if the
Print button in the dialog box was clicked. If the button was clicked, the macro prints after the dialog
box is dismissed.
- Rename a button on your (Microsoft Excel version 5.0 or 7.0) dialog
sheet to "print" (without the quotation marks).
NOTE: Use the Name box on the formula bar to rename the button.
- Assign the Set_Flag macro to this Print button.
- Create a second button on the dialog sheet, rename it "other".
- Assign the Set_Flag macro to this other button.
- To be able to test the use of the other button, create another Excel 5.0/7.0 dialog sheet and rename the sheet "Other".
- Enter the following macros into a new module sheet in your workbook.
' 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
- Run the Show_Dialog macro.
- Click the Print button in your custom dialog box that was assigned to the Set_Flag macro.
- Click OK in your dialog box to dismiss the dialog box.
Print_Macro runs and "Your Print Macro here" message appears in a
message box.