Free Support Forum - aspose.com

Exposing IMailMergeDataSource in MergeFieldEventHandlers

We are using the MailMerge functionality quite a lot and most of the times we are merging data stored in a custom type. In order to achieve this, we had to implement our own IMailMergeDataSource which gets the value of mergefields using reflection. So far so good :).

However, during the MailMerge process the only way to 'hook' into the process is to use a custom MergeFieldEventHandler. Within such an eventhandler we get some information about the field being merged and it's value (found within the datasource using the (custom) MailMergeDataSource). What we are missing is a reference to the actual MailMergeDataSource which was used to get the field value from. If that reference could be added to the MailMergeEventArgs we could:

  • Determine whether the field was found within the datasource (whenever multiple datasources are merged with the same template). This is usefull information whenever you would like to adjust the document based on the information of the value of a mergefield (e.g. delete the parent row of a mergefield whenever it's value is 'null'.)
  • Make use of information stored in e.g. custom attributes which are added to the properties of the custom type being merged. (This gives us the flexibility to format the value of a field according to the settings of it's attribute.)
  • and more...

So in short my question is: Can you add/expose a reference to the IMailMergeDataSource, that was used to get the value of mergefield, within the MergeFieldEventArgs?

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. We will consider adding such functionality in one of future versions. Your request has been linked to the appropriate issue. You will be notified as soon as it is resolved.

Best regards,

Hi there,


Regarding your original request, I’m afraid it’s still unresolved, however if you are still in need of this functionality I can provide you a simple work around for use in the mean time.

You can achieve what you are looking for just by keeping a global reference to the active IMailMergeDataSource instance. This way you can refer to this instance which provides the data to be merged within your handler.

<span style=“font-size:10.0pt;line-height:115%;font-family:
“Courier New”;color:green;mso-no-proof:yes”>// Define this as a static global
variable.<span style=“font-size:10.0pt;line-height:115%;font-family:
“Courier New”;mso-no-proof:yes”>

public static IMailMergeDataSource mCurrentDataSource = null;<o:p></o:p>

<span style=“font-size:10.0pt;line-height:115%;font-family:
“Courier New”;mso-no-proof:yes”>

// Update this reference with the instance of the data source which is providing the current data.

<span style=“font-size:
10.0pt;font-family:“Courier New”;color:blue;mso-no-proof:yes”>public<span style=“font-size:10.0pt;font-family:“Courier New”;mso-no-proof:yes”> bool GetValue(string
fieldName, out object
fieldValue)<o:p></o:p>

{

mCurrentDataSource = this;
// Retrieve and return data here.
}


// You can now find the IMailMergeDataSource instance that is providing the data for the current record.

public void FieldMerging(FieldMergingArgs args)

{

string prop = mCurrentDataSource.MyCustomProperty;

}



Thanks,