Partager via


WorkbookBase.BeforePrint Event

Definition

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

public:
 event Microsoft::Office::Interop::Excel::WorkbookEvents_BeforePrintEventHandler ^ BeforePrint;
public event Microsoft.Office.Interop.Excel.WorkbookEvents_BeforePrintEventHandler BeforePrint;
member this.BeforePrint : Microsoft.Office.Interop.Excel.WorkbookEvents_BeforePrintEventHandler 
Public Custom Event BeforePrint As WorkbookEvents_BeforePrintEventHandler 

Event Type

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.

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.");
    }
}
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

Applies to