Hello,
When I open “INF AP15 - Emergency evacuation controller CR 29 - BubbleGum Edit.docx” file in Microsoft Word it shows me “” request (please look at the screenshot image below). And I have to select “BubbleGum Editor.xlsx” file as a data source manually.
So, can I use Aspose.Words to process this request and select the data source file automatically?
Mikhael
word request
two files.zip (251.4 KB)
@Mikhael To avoid this alert you should clear the document’s mail merge settings:
Document doc = new Document(@"C:\Temp\in.docx");
doc.MailMergeSettings.Clear();
doc.Save(@"C:\Temp\out.docx");
you can fill the document with data by executing mail merge before clearing the document mail merge settings.
Hello @alexey.noskov ,
Thank you for fast answer!
In general I have no idea what kind of field in DOCX file should be filled.
So, is it possible to enumerate all fields in the DOC or DOCX file I try open and then apply “mailmerge” for each field? May be you have a code snippet for this task?
Mikhael
@Mikhael You can use the following code to get merge field names from the document:
Document doc = new Document(@"C:\Temp\in.docx");
foreach (string s in doc.MailMerge.GetFieldNames())
Console.WriteLine(s);
but generally, you should simply read your data source into DataSet
and then use it as a data source for executing mail merge as shown in our documentation.
Hello @alexey.noskov ,
Thank you for this info!
As you can see on the screenshot image below, I have a DOCX file, that contains “MailMergeSettings” object incide that contains all data to execute MailMerge and to fill all fields.
Here is an another code snippet I use to call MS Word API and it works:
WordDoc = WordApp.Documents.Open(m_sDocumentFile, , False, , , , , , , , )
WordDoc.MailMerge.OpenDataSource(Name:=sDataFile, ReadOnly:=True) ' "Select table" window appears!!!
WordDoc.MailMerge.Execute() ' no parameters!
In Aspose.Words API I only found a function doc.MailMerge.Execute(...)
that required a list of field names (no problem) and list of values. So, does it mean, that I should interpret MailMergeSettings
to request the soucre data file to get values bofore I can call doc.MailMerge.Execute(...)
?
Mikhael
@Mikhael Yes, to execute mail marge using Aspose.Words you should get the required data and use them as parameters of MailMerge.Execute
method.
Hello @alexey.noskov ,
OK, I see. Thank you for your support!
Mikhael
1 Like