Document.BeforePrint Event (2007 System)

Occurs before the document is printed.

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

Syntax

'Declaration
Public Event BeforePrint As CancelEventHandler
'Usage
Dim instance As Document 
Dim handler As CancelEventHandler 

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

Remarks

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

Examples

The following code example displays a message before the document is printed that asks whether you want to print the document.

This version is for a document-level customization.

Private Sub DocumentBeforePrint()
    AddHandler Me.BeforePrint, AddressOf ThisDocument_BeforePrint
End Sub 

Private Sub ThisDocument_BeforePrint(ByVal sender As Object, ByVal e As System. _
    ComponentModel.CancelEventArgs)
    If MessageBox.Show("Do you want to print the document?", "BeforePrint", _
        MessageBoxButtons.YesNo) = DialogResult.No Then
        e.Cancel = True 
    End If 
End Sub
private void DocumentBeforePrint()
{
    this.BeforePrint += new System.ComponentModel.CancelEventHandler(ThisDocument_BeforePrint);
}

void ThisDocument_BeforePrint(object sender, System.ComponentModel.CancelEventArgs e)
{
    if (MessageBox.Show("Do you want to print the document?", "BeforePrint",
        MessageBoxButtons.YesNo) == DialogResult.No)
    {
        e.Cancel = true;
    }
}

This version is for an application-level add-in.

Private Sub DocumentBeforePrint()
    Dim vstoDoc As Document = Me.Application.ActiveDocument.GetVstoObject()
    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 = Me.Application.ActiveDocument.GetVstoObject()
    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
private void DocumentBeforePrint()
{
    Document vstoDoc = this.Application.ActiveDocument.GetVstoObject();
    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;
    }
}

.NET Framework Security

See Also

Reference

Document Class

Document Members

Microsoft.Office.Tools.Word Namespace

Change History

Date

History

Reason

July 2008

Added a version of the code example for an application-level add-in.

SP1 feature change.