Share via


WorkbookBase.BeforePrint Event

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

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

Syntax

'Declaration
Public Event BeforePrint As WorkbookEvents_BeforePrintEventHandler
public event WorkbookEvents_BeforePrintEventHandler BeforePrint

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

WorkbookBase Class

Microsoft.Office.Tools.Excel Namespace