MailItem.Write event (Outlook)

Occurs when an instance of the parent object is saved, either explicitly (for example, using the Save or SaveAs methods) or implicitly (for example, in response to a prompt when closing the item's inspector).

Syntax

expression. Write( _Cancel_ )

expression A variable that represents a MailItem object.

Parameters

Name Required/Optional Data type Description
Cancel Required Boolean (Not used in VBScript). False when the event occurs. If the event procedure sets this argument to True, the save operation is not completed.

Remarks

In Microsoft Visual Basic Scripting Edition (VBScript), if you set the return value of this function to False, the save operation is not completed.

Example

This Visual Basic for Applications (VBA) example uses the Write event and warns the user that the item is about to be saved and will overwrite any existing item and, depending on the user's response, either allows the operation to continue or stops it. If this event is canceled, Microsoft Outlook displays an error message. Therefore, you need to capture this event in your code. One way to do this is shown below. The sample code must be placed in a class module such as ThisOutlookSession, and the Initialize_Handler() subroutine must be called before the event procedure can be called by Outlook.

Public WithEvents myItem As Outlook.MailItem 
 
 
 
Private Sub myItem_Write(Cancel As Boolean) 
 
 Dim myResult As Integer 
 
 myItem = "The item is about to be saved. Do you wish to overwrite the existing item?" 
 
 myResult = MsgBox(myItem, vbYesNo, "Save") 
 
 If myResult = vbNo Then 
 
 Cancel = True 
 
 End If 
 
End Sub 
 
 
 
Public Sub Initialize_Handler() 
 
 Const strCancelEvent = "Application-defined or object-defined error" 
 
 
 
 On Error GoTo ErrHandler 
 
 
 
 Set myItem = Application.ActiveInspector.CurrentItem 
 
 myItem.Save 
 
 Exit Sub 
 
 
 
 ErrHandler: 
 
 MsgBox Err.Description 
 
 If Err.Description = strCancelEvent Then 
 
 MsgBox "The event was cancelled." 
 
 End If 
 
End Sub

See also

MailItem Object

Support and feedback

Have questions or feedback about Office VBA or this documentation? Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.