Share via


WorkbookBase.BeforePrint 事件

在列印活頁簿 (或其中任何資料) 前發生。

命名空間:  Microsoft.Office.Tools.Excel
組件:  Microsoft.Office.Tools.Excel.v4.0.Utilities (在 Microsoft.Office.Tools.Excel.v4.0.Utilities.dll 中)

語法

'宣告
Public Event BeforePrint As WorkbookEvents_BeforePrintEventHandler
public event WorkbookEvents_BeforePrintEventHandler BeforePrint

範例

下列程式碼會示範 BeforePrint 事件的處理常式。 這個事件處理常式會提示使用者繼續或取消列印作業。 如果使用者取消列印作業,則 WorkbookEvents_BeforePrintEventHandler 事件處理常式的 Cancel 參數會設為 true,因此 Microsoft Office Excel 便不會列印這個活頁簿。

這是示範文件層級自訂的範例。

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 安全性

請參閱

參考

WorkbookBase 類別

Microsoft.Office.Tools.Excel 命名空間