Word cannot find its data source

Hello, I am generating a mail merge template and a data source (CSV) file with Aspose.Words version 14.12. I want to allow my client to edit the template within Word. When I open the template with Word 2013, I receive an error from Word: APC_A.docx is a mail merge main document. Word cannot find its data source, c:\users\jschmid\Desktop\APC_A.csv.

Needless to say, the file does exist. I can use Word to remove the data source, then manually link to the data source file and it works as expected.

When creating the template, I’m using the following code to specify the data source:

mySrcDoc.MailMergeSettings.DataSource = "c:\users\jschmid\Desktop\APC_A.csv"
mySrcDoc.MailMergeSettings.MainDocumentType=Settings.MailMergeMainDocumentType.FormLetters
mySrcDoc.MailMergeSettings.DataType = Settings.MailMergeDataType.TextFile
mySrcDoc.MailMergeSettings.Query = "SELECT * FROM c:\users\jschmid\Desktop\APC_A.csv"

I’m sure its something silly that I have not set properly, but I cannot find it. I even manually linked a data source in Word and opened that with Aspose.Words, but the settings look identical to what I used. I’ve attached the document and test data file I created.

Thanks,

Hi John,

Thanks for your inquiry. Please use the MailMergeSettings.Clear
method to clear the mail merge settings in such a way that when the
document is saved, no mail merge settings will be saved and it will
become a normal document.

Please use the following code example to achieve your requirements. Hope this helps you.

Document doc = new Document(MyDir + "APC_A.docx");
doc.MailMergeSettings.Clear();
string query = "SELECT * FROM 'APC_A'";
MailMergeSettings mms = doc.MailMergeSettings;
mms.MainDocumentType = MailMergeMainDocumentType.MailingLabels;
mms.DataType = MailMergeDataType.Native;
mms.DataSource = @"C:\Temp\APC_A.csv";
mms.Query = query;
mms.LinkToQuery = false;
mms.ViewMergedData = true;
doc.Save(MyDir + "Out.docx");

Thank you Tahir. That sample resolved the issue.

Hi John,

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