We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Looping through dataset using for loop and mailmerge.execute() or just using a datatable with executewithregions()

I am working on a feature that gets various document types (pictures, text, etc), and puts these into a report. There will be some types of content that Aspose.words can not handle (for example audio or video).

I was wondering what the recomendation is between using my own loop, and calling mailmerge.execute OR doing a document.mailmerge.executewithregions() on the datatable I have. The only thing I can see is where i check for 'invalid' file types (in my code, rather than handling a possible error in the executewithregions().

The main reason I ask is using any object type in mailmerge.execute().

I will have a MergeFieldEventHandler() and MergeImageFieldEventHandler() - will it know which one to use?

Hi

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

Thanks for your request. First of all, you should note, there is no sense to execute simple mail merge multiple times. Let’s consider a simple example: you have a template, which contains two mergefields with the same name, also you have a datasource , which contains two records. If you execute simple mail merge for first record in your datasource, both of mergefields in your document will be filled with data. So your document no longer contains mergefiels, that is why there is no sense to execute simple mail merge second time.

So, if you need to repeat some part of document for each record in your datasource, you should use mail merge with regions:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/mail-merge-with-regions-explained.html

If whole document, should be repeated for each record in your datasource, you should use simple mail merge:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/simple-mail-merge-explained.html

Regarding MergeField and MergeImageField event handlers, MergeField event handler is called when a simple mergefield is filled with data, MergeImageField is called when Image mergefield is filled with data:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/howto-insert-images-from-a-database.html

Best regards.

I have done the mailmergewithregions plenty of times through this project, so I am familiar with it.

The only problem though is that the fields in that 'table' may be different types from the DB - a document, text, or a picture. If I have them all handled in my main handler, how for example, would the image then jump to the image handler?

I am thinking my best route for this as of now is to place each 'article' into a new document (no matter what it is), and then just merge all the documents together. In that case, would the loop be better?

Hi

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

Thank you for additional information. If you would like to use MergeImageField event handler, you need to have mergefields with special names in your template, like “Image:MyField”. Please see the following link for more information:

http://www.aspose.com/documentation/.net-components/aspose.words-for-.net-and-java/aspose.words.reporting.mergeimagefieldeventhandler.html

Best regards.

Can one handler (ie the normal one) call an image handler if it comes accross an image field?

As I posted in my above post, since all types of different content would be going in to the same repeated merge field, im thinking it would work best to put these into a document, and then merge all documents.

Basically, is there a type of field that I could send it text/document/image, and it work? For example, something like how you can insert HTML at a merge field, could I insert an image, or insert text, even if its not an Image:fieldname

Hi

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

Thank you for additional information. Of course you can use MergeField event handler to insert images. For instance, see the following code:

// Open template.

Document doc = new Document(@"Test001\in.doc");

// Add MergeField event handler.

doc.MailMerge.MergeField += MailMerge_MergeField;

// Execute mail merge.

doc.MailMerge.Execute(new string[] {"MyImage"}, new object[] {@"C:\Temp\test.jpg"});

// Save output document.

doc.Save(@"Test001\out.doc");

=================================================================================================

void MailMerge_MergeField(object sender, Aspose.Words.Reporting.MergeFieldEventArgs e)

{

if(e.FieldName == "MyImage")

{

DocumentBuilder builder = new DocumentBuilder(e.Document);

// Move DocumentBuilder cursor to the mergefield.

builder.MoveToField(e.Field, true);

// Insert an image (In this canse i am sure that field value is path to image).

builder.InsertImage(e.FieldValue.ToString());

// Remove field because it is no longer needed.

e.Field.Remove();

}

}

Hope this helps.

Best regards.