_XDocumentEventSink2_Event.OnSaveRequest event

Office 2013 and later

Occurs when the save operation is invoked from the Microsoft InfoPath user interface or by using the Save or SaveAs methods.

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

event _XDocumentEventSink2_OnSaveRequestEventHandler OnSaveRequest

If the ReturnStatus property of the SaveEventObject object is set to false, InfoPath cancels the save operation. If an error occurs in the code for the OnSaveRequest event, InfoPath ignores the error and relies on the ReturnStatus property of the SaveEventObject object If the ReturnStatus property is not explicitly set, the default value of false is used.

The ReturnStatus property works in conjunction with the IsCancelled property when the InfoPath form is closing. If the document has changes that have not been saved and the user cancels the save operation, the IsCancelled property can be set to true to allow InfoPath to close.

public void OnSaveRequest(SaveEvent e)
  if(thisXDocument.DOM.selectSingleNode("//my:field2").text == "")
   thisXDocument.UI.Alert("Field2 is empty.\nYou cannot save the form.");
e.IsCancelled = e.PerformSaveOperation();
// set ReturnStatus to false if you want InfoPath
// to show an additional error message
e.ReturnStatus = true;