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

Free Support Forum - aspose.cloud

Automate Word Mail Merge From XML Data in Node.js Using Aspose.Words REST API

Hi

I have a word document which has 5 merge fields in a table, using words catalog template, cannot get it to mailmerge in the API no errors produces document. It might be the structure of my xml, any ideas.

Hope that makes sense.

@IansResearch

You can automate Word Mail Merge from XML data in Node.js using Aspose.Words REST API as follows. If you face any issue then please share your template and data file along with the sample code. We will look into it and will guide you. Please also check the following documentation for sample templates and data.

Steps to Mail Merge MS Word with XML Datasource in Node.js

  • Create a free account to get Credentials of aspose.cloud
  • Install Aspose.Words Cloud SDK for Node.js from NPM
  • Create a script file for code and import asposewordscloud module
  • Initialize Aspose.Words Cloud API
  • Create paths variables for the Template file and XML data file
  • Create ExecuteMailMergeOnlineRequest object and set the required properties
  • Call executeMailMergeOnline API method to Mail Merge the Word document
  • Save output document to the local drive

Node.js Code to Automate Word Mail Merge from XML data

const { WordsApi, ExecuteMailMergeOnlineRequest } = require("asposewordscloud");
var fs = require('fs');

// initialize the API
// Please get your App Key and App SID from https://dashboard.aspose.cloud/#/apps.
wordsApi = new WordsApi("xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxx");
// set paths of Template and data file
const localDocumentFile = "C:/Temp/MailMergeSamples/coverageChangeTemplate.docx";
const localDataFile = "C:/Temp/MailMergeSamples/coverageChange.json";

// Mail Merge
const request = new ExecuteMailMergeOnlineRequest({
      template: fs.createReadStream(localDocumentFile),
      data: fs.createReadStream(localDataFile),
      withRegions: true,
      cleanup: "EmptyParagraphs,UnusedRegions,UnusedFields,ContainingFields,RemoveTitleRow,RemoveTitleRowInInnerTables" 	
	});
                       

wordsApi.executeMailMergeOnline(request).then((result) => {    
    console.log(result.path);   
	fs.writeFileSync('C:/Temp/MailMerge_cust.docx', result.body);
    console.log('Completed...');	
}).catch(function(err) {
    // Deal with an error
    console.log(err);
});

Hi

Managed to get it working, thank you check examples again. Another question is possible to put mailmerge fields in the header part of a document.

Thank you

@IansResearch

Yes, you can add Mail Merge fields in the header of the template. Please find a sample template and data file for reference.HtmlImageTemplate.zip (13.6 KB)