FormEvents.Save event

Occurs when the Save or Save As commands are used from the user interface, or when the Save and SaveAs methods are used.

Namespace:  Microsoft.Office.InfoPath
Assembly:  Microsoft.Office.InfoPath (in Microsoft.Office.InfoPath.dll)


Public MustOverride Event Save As SaveEventHandler
Dim instance As FormEvents
Dim handler As SaveEventHandler

AddHandler instance.Save, handler
public abstract event SaveEventHandler Save


Exception Condition

The developer attempted to bind the event in some location other than the InternalStartup method.



The Save event is not meant to be instantiated by the developer in form code. You should only add event handlers for form-level events from the Microsoft InfoPath design mode user interface. When you add an event handler to your form template from the design mode user interface, InfoPath generates code in the InternalStartup method of your form code file using the EventManager class and the member of the FormEvents class to bind the event to its event handler. For information on how to add event handlers in InfoPath design mode, see How to: Add an Event Handler.

The Save event is raised only if the form template has the Save using custom code option set in the Form Options dialog box.

The Save event is bound using the SaveEventHandler delegate.

The Save event can be cancelled by using the CancelableArgs property of the SaveEventArgs class to set the Cancel property to true.

The SaveEventArgs object, which is passed as a parameter to an event handler for the Save event, provides properties and methods that can be used to get the form's file name, determine save status, and perform the save operation.

This type or member can be accessed only from code running in forms opened in Microsoft InfoPath Filler.


In the following example, the event handler for the Save event checks to see if Field2 is empty, and if it is not performs a save operation. If it is empty, it displays a message and cancels the save operation.

public void FormEvents_Save(object sender, SaveEventArgs e)
   // Check to see if Field2 is empty.
   XPathNavigator reqField = 
   if(reqField.ToString() == "")
      MessageBox.Show("Field2 is empty.\nYou cannot save the form.");
      e.CancelableArg.Cancel = true;
   // The Dirty property will be set to false if save is successful.
      e.CancelableArgs.Cancel = false;
Public Sub FormEvents_Save(ByVal sender As Object, _
   ByVal e As SaveEventArgs)
   ' Check to see if Field2 is empty.
   Dim reqField as XPathNavigator = 
      CreateNavigator().SelectSingleNode("/my:myFields/my:field2", _
   If(reqField.ToString() = "") Then
      MessageBox.Show("Field2 is empty" & vbNewLine & _
         "You cannot save the form.")
      e.CancelableArgs.Cancel = True
   ' The Dirty property will be set to false if save is successful.
   e.CancelableArgs.Cancel = False
   End If
End Sub

See also


FormEvents class

FormEvents members

Microsoft.Office.InfoPath namespace