Workbook.BeforePrint Event (2007 System)

Occurs before the workbook (or anything in it) is printed.

Namespace:  Microsoft.Office.Tools.Excel
Assembly:  Microsoft.Office.Tools.Excel.v9.0 (in Microsoft.Office.Tools.Excel.v9.0.dll)

Syntax

'Declaration
Public Event BeforePrint As WorkbookEvents_BeforePrintEventHandler
'Usage
Dim instance As Workbook 
Dim handler As WorkbookEvents_BeforePrintEventHandler 

AddHandler instance.BeforePrint, handler
public event WorkbookEvents_BeforePrintEventHandler BeforePrint
public:
 event WorkbookEvents_BeforePrintEventHandler^ BeforePrint {
    void add (WorkbookEvents_BeforePrintEventHandler^ value);
    void remove (WorkbookEvents_BeforePrintEventHandler^ value);
}
JScript does not support events.

Examples

The following code example demonstrates a handler for the BeforePrint event. The event handler prompts the user to either continue with the print operation or cancel it. If the user cancels the print operation, then the Cancel parameter of the WorkbookEvents_BeforePrintEventHandler event handler is set to true so that Microsoft Office Excel does not print the workbook.

This example is for a document-level customization.

Sub ThisWorkbook_BeforePrint(ByRef Cancel As Boolean) _
    Handles Me.BeforePrint

    If DialogResult.No = MessageBox.Show("Are you sure " & _
        "you want to print the workbook?", _
        "Sample", MessageBoxButtons.YesNo) Then
        Cancel = True
        MessageBox.Show("Print is canceled.")
    End If 
End Sub
private void WorkbookBeforePrint()
{
    this.BeforePrint +=
        new Excel.WorkbookEvents_BeforePrintEventHandler(
        ThisWorkbook_BeforePrint);
}

void ThisWorkbook_BeforePrint(ref bool Cancel)
{
    if (DialogResult.No == MessageBox.Show("Are you sure " +
        "you want to print the workbook?",
        "Example", MessageBoxButtons.YesNo))
    {
        Cancel = true;
        MessageBox.Show("Print is canceled.");
    }
}

.NET Framework Security

See Also

Reference

Workbook Class

Workbook Members

Microsoft.Office.Tools.Excel Namespace