Attach mailmerge data source WITHOUT executing mailmerge

Hi,

Using Aspose.Words v11.3 under .NET, is it possible to attach a data source to a document without executing the mailmerge itself?

Although the majority of our users will want the resultant document straight away (which I can provide by using “

doc.MailMerge.Execute(datasource)
”), there a some users who need the document to be sent to them in an unmerged state with the data source embedded. This allows them to modify the document appropriately and then run the mailmerge.

To add more information: at present, the datasource that is being used with the execute command is a simple DataTable. I can’t any way to attach this DataTable to the document without merging. What I can see is “
doc.MailMergeSettings.DataSource
” property but this appears to be wanting a file path, which is not appropriate in this situation… as I really don’t want to have to save the data into a local file first.

Edit:

I’m starting to think this through more, and I’m coming to the conclusion that my question might be fundamentally flawed - and that it’s not actually possible to attach/embed a datasource within a document.

The reason I’m asking for this functionality is because I’m convering our ASP.NET application away from using client-side Word automation, which is currently working via an old VB6 ActiveX component. The OCX was written in such a way that the “template document” was loaded by Word, and then it created a data source document and attached it - and then the mail merge was run if required, otherwise it was left in the state of the original document with attached data source.

Office 2013 is screwing us up, because it puts documents automatically into “View Mode”, and to work properly requires a property to be set to put it into “Edit Mode”. However, the idea of updating, distributing and install a new ocx/dll is a horrifying one, and that is why I’m trying to use Aspose instead.

Can anybody confirm that I’m effectively asking for Aspose to do something that can’t be done??

Many thanks,
Tom

Hi Tom,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does. Aspose.Words can generate documents from templates with mail merge fields. The data from an external source like a database or file is placed into these fields and formatted, and the resulting document is saved in the folder you specify.

Data can come from a source in a variety of formats supported by ADO.NET. It can be DataTable, DataView, DataSet, IDataReader or an array of values. If you have your data in XML format, then you can load it into a DataSet and merge with the DataSet.

For mail merge process, you need a template document and data source. In your case, you can use MailMergeSettings.DataSource property to specify the path to the mail-merge data source. Please check following code example for your kind reference. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");

doc.MailMergeSettings.Clear();

string query = "SELECT * FROM 'sheet1'";

MailMergeSettings mms = doc.MailMergeSettings;

mms.MainDocumentType = MailMergeMainDocumentType.MailingLabels;

mms.DataType = MailMergeDataType.Native;

mms.DataSource = @"C:\Temp\in.csv";

mms.Query = query;

mms.LinkToQuery = false;

mms.ViewMergedData = true;

doc.Save(MyDir + "Out.docx");


Hi Tahir,

Thanks for your reply, which has completely missed the ENTIRE point of my post.

I know how to merge, and had that working within about 10 minutes of my first attempt… but that was not what I was asking.

I realise now that it is impossible to have a single Word document with a data source embedded, as it make no sense at all. A document can only have a data source attached when it’s being used within Word… the concept of passing the data to Word from within the document is just stupid.

As such I have already made the changes to my system to cope with this.

Thanks for you “help”,
Tom

Hi Tom,

It is nice to hear from you that you know about Aspose.Words mail merge process. Yes, your concept about Word document and data source is correct.

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.