Share via


FormEvents.Merge Event (Microsoft.Office.InfoPath)

Occurs when the Merge Forms command is invoked from the user interface, or InfoPath is started with the /aggregate command-line switch.

Namespace: Microsoft.Office.InfoPath
Assembly: Microsoft.Office.InfoPath (in microsoft.office.infopath.dll)

Syntax

'Declaration
Public Event Merge As MergeEventHandler
'Usage
Dim instance As FormEvents
Dim handler As MergeEventHandler

AddHandler instance.Merge, handler
public abstract event MergeEventHandler Merge

Exceptions

Exception type Condition

InvalidOperationException

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

Remarks

Important

The Merge 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 Office InfoPath 2007 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 Merge event is bound using the MergeEventHandler delegate.

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

If you cancel the event, how InfoPath works depends on whether or how you set the Message and MessageDetails properties as described in the following table.

Message and MessageDetailsSetting

Dialog Box and Text Displayed

Set only the Message property to a message string

Your message and "Do you want to continue?" with Yes, Yes to All, and Cancel buttons.

Set only the MessageDetails property to a message string

"Microsoft Office InfoPath has encountered a problem merging the following form: formname.xml." and your detailed message with Yes, Yes to All, and Cancel buttons.

Set both the Message and MessageDetails properties to a message string

Your message and detailed message with Yes, Yes to All, and Cancel buttons.

Don't set the Message and MessageDetails properties, or set them both to null or an empty string

No dialog box is displayed.

The behaviors when a user clicks the Yes, Yes to All, and Cancel buttons are as follows:

Button Clicked

Merge Behavior

Yes button

The merge operation takes place and the user has to click Yes for each individual form being merged.

Yes to All button

The merge operation takes place and all selected forms are merged.

Cancel button

The merging of all forms is cancelled. If the user clicked Yes for any forms before clicking Cancel, the merging of those forms is also cancelled.

The MergeEventArgs object is passed as a parameter to the event handler for the Merge event of a form. The MergeEventArgs object provides properties that can be used during the Merge event to programmatically interact with a form's underlying XML document and to determine merge properties such as the number of files being merged. The properties that the MergeEventArgs object provides are available only during this event.

During a single merge forms operation, the Merge event will occur (and the code in its event handler will run) once for each file being merged.

Important

In the InfoPath 2003 object model, there is an OnAfterImport event that occurs after the merge operation is completed. The Microsoft Office InfoPath 2007 object model does provide an equivalent event. However, the same functionality can be achieved in an event handler for the Merge event by comparing the values of the Index and Count properties of the MergeEventArgs class to confirm that the merge operation is complete, before performing any "after merge" operations.

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

Example

In the following example, the event handler for the Merge event sets variables to indicate the status of the merge operation, calls the MergeForm(XPathNavigator) method of the XmlForm class to perform the merge operation, and displays a message when merging is finished.

private bool _merging = false;
public void FormEvents_Merge(object sender, MergeEventArgs e)
{
   // Set global property to indicate that forms are being merged.
   if (e.Index == 0)
   {
      _merging = true;
   }
   // Merge the current form.
   MergeForm(e.Xml);
   e.CancelableArgs.Cancel = false;

   // Check to see if merging is finished.
   if ((e.Index + 1) == e.Count)
   {
      _merging = false;
      MessageBox.Show("Your request to merge " + e.Count + 
         " files is now complete.");
      // Perform any "after merge" operations here.
   }
}
Private _merging As Boolean = False
Public Sub FormEvents_Merge(ByVal sender As Object, _
   ByVal e As MergeEventArgs)
   ' Set global property to indicate that forms are being merged.
   If (e.Index = 0) Then
      _merging = True
   End If

   ' Merge the current form.
   MergeForm(e.Xml)
   e.CancelableArgs.Cancel = False

   ' Check to see if merging is finished.
   If ((e.Index + 1) = e.Count) Then
      _merging = False
      MessageBox.Show("Your request to merge " + e.Count + 
         " files is now complete.")
      ' Perform any "after merge" operations here.
   End If
End Sub

See Also

Reference

FormEvents Class
FormEvents Members
Microsoft.Office.InfoPath Namespace