Free Support Forum -

MailMerge.Execute takes too long to execute and then thows error System.OutOfMemoryException

Hello there,

I am simply testing my application report generation using Aspose for any performance issue and getting System.OutOfMemoryException when calling Document.Save() after mail merge.

Here is the scenario:

Below is the code that’s calling MailMerge.Execute

PropertyTestMailMergeDataSource mmDataSource = new PropertyTestMailMergeDataSource(propertyList);

foreach (Property objProperty in propertyList)
VendorsDataSource mmDataSource2 = new VendorsDataSource(objProperty.PropertyVendors);

foreach (Property objProperty in propertyList)
BuyersDataSource mmDataSource1 = new BuyersDataSource(objProperty.PropertyBuyers);

First issue: However, for second set of child objects (code in red color), it just takes too long to execute.
Please note that it’s happening only in case of my last MailMerge.Execute call. So, if I move the PropertyBuyers code up and PropertyVendors code down, it will happend for PropertyVendors.

Second issue: And after this code when I call document.Save(fileName), it throws error:


Stack Trace:
at System.IO.MemoryStream.set_Capacity(Int32 value)
at System.IO.MemoryStream.EnsureCapacity(Int32 value)
at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
at System.IO.StreamWriter.Write(String value)
at System.Xml.XmlTextWriter.WriteStartElement(String prefix, String localName, String ns)
at System.Xml.XmlWriter.WriteStartElement(String localName)…
at Aspose.Words.Paragraph.Accept(DocumentVisitor visitor)
at Aspose.Words.CompositeNode.Ս(DocumentVisitor Վ)
at Aspose.Words.Body.Accept(DocumentVisitor visitor) …
at Aspose.Words.Document.ԅ(Stream Ӿ, String Ӽ, SaveFormat Ԇ)
at Aspose.Words.Document.Save(String fileName, SaveFormat fileFormat)
at Aspose.Words.Document.Save(String fileName)

Please guide.

Thank you!


Thanks for your inquiry. Could you please create sample application, which will allow me to reproduce the problem on my side? I will check the issue and try to help you.

Best regards,

Please find attached the .cs file containing entities and Mail Merge code (main method call is ProcessReportFinalOutput())
Also attached is the document for which I am running Mail Merge.

Edited: I have re-attached the .cs file (earlier one was missing some methods)


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

Thank you for additional information. Why do you execute mail merge several times? I do not think, that this approach will give you an expected result because once you execute mail merge all mergefields, which are present in your data source, will be filled with data. So when you execute mail merge second time with the same data source, there will be no mergefields to fill.

As I can see you use .NET 3.0 or higher, so I think you can try using anonymous types in your case and create only one IMailMergeDataSource.

Please let me know if you need more assistance.

Best regards.

Thank you Alexey for your response.

Yes, you are right that I am calling MailMerge.Execute multiple times,
but they are not with same data source. If you notice I am running each time with different data source.

Right now, I am just implementing the way mentioned in following post:
<a href="


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

Thank you for additional information. Yes, you are right, you execute mail merge with different data sources, but set of columns in these data sources is the same. So, finally, you will get data only from the first data source in the output document.

I think, the technique mentioned in the thread, you have referenced to, will fit to your requirements. In addition, I think, you should look in the anonymous types direction. Maybe this approach can simplify your task.

Best regards.