Write Event

Occurs when a Microsoft Outlook item 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).

Subobject**_Write(Cancel As Boolean)**

*object   * An expression that evaluates to one of the objects in the Applies To list. In VBScript, use the word Item.

*Cancel   * Optional 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 Microsoft 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 Initalize_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

Applies to | AppointmentItem Object | ContactItem Object | DistListItem Object | DocumentItem Object | JournalItem Object | MailItem Object | MeetingItem Object | PostItem Object | RemoteItem Object | ReportItem Object | TaskItem Object | TaskRequestAcceptItem Object | TaskRequestDeclineItem Object | TaskRequestItem Object | TaskRequestUpdateItem Object

See Also | Close Method | CustomAction Event | CustomPropertyChange Event | Forward Event | Open Event | PropertyChange Event | Read Event | Reply Event | ReplyAll Event | Send Method | Using events with Automation