Document.BeforePrint Event

Definition

Occurs before the document is printed.

public:
 event System::ComponentModel::CancelEventHandler ^ BeforePrint;
event System.ComponentModel.CancelEventHandler BeforePrint;
member this.BeforePrint : System.ComponentModel.CancelEventHandler 
Event BeforePrint As CancelEventHandler 

Event Type

Examples

The following code example displays a message before the document is printed that asks whether you want to print the document. This example is for an application-level add-in.

private void DocumentBeforePrint()
{
    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    vstoDoc.BeforePrint += new System.ComponentModel.CancelEventHandler(ThisDocument_BeforePrint);
}

void ThisDocument_BeforePrint(object sender, System.ComponentModel.CancelEventArgs e)
{
    if (System.Windows.Forms.MessageBox.Show("Do you want to print the document?", "BeforePrint",
        System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
    {
        e.Cancel = true;
    }
}
Private Sub DocumentBeforePrint()
    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    AddHandler vstoDoc.BeforePrint, AddressOf ThisDocument_BeforePrint
End Sub

Private Sub ThisDocument_BeforePrint(ByVal sender As Object, ByVal e As System. _
    ComponentModel.CancelEventArgs)
    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    If System.Windows.Forms.MessageBox.Show("Do you want to print the document?", "BeforePrint", _
        System.Windows.Forms.MessageBoxButtons.YesNo) = System.Windows.Forms.DialogResult.No Then
        e.Cancel = True
    End If
End Sub

Remarks

To prevent the document from printing, set the Cancel argument of the provided CancelEventArgs object to true.

Applies to