Microsoft Word Mail Merge in C# .NET Core 2.1 Framework using Aspose.Words REST API

I’m trying to do a simple word merge and not having any luck at all. The code runs through and places the word merge doc in my storage location, but after passing the word merge fields, it never seems to populate the actual word merge fields.

Here’s my code:wordmerge.PNG (79.5 KB)
Fields.PNG (5.8 KB)

        string BaseProductUri = @"https://api.aspose.cloud";
        Aspose.Words.Cloud.Sdk.Configuration configuration = new Aspose.Words.Cloud.Sdk.Configuration { 
        ApiBaseUrl = BaseProductUri + "/v1.1", AppKey = "XXXXXX", AppSid = "XXXXX" };
        WordsApi _asposewords = new WordsApi(configuration);
        StorageApi _storage = new StorageApi("XXXXX", "XXXX");


        string localName = "MailMerge.docx";
        string remoteName = DateTime.Now.ToLongTimeString() + TestPostDocumentExecuteMailMerge.docx";
        string BaseTestPath = "C:\\Users\\bdubose\\Documents\\aspose\\";
        string datafolder = "C:\\Users\\";
        string fullName = Path.Combine(BaseTestPath, remoteName);
        string destFileName = Path.Combine(datafolder, remoteName);
        string data = System.IO.File.ReadAllText(BaseTestPath + "CoverLetterFields.txt");

        Stream _stream = new FileStream(BaseTestPath + localName, FileMode.Open);


        PutCreateRequest req = new PutCreateRequest();
        req.Path = fullName;
        req.File = _stream;
        req.VersionId = null;
        req.Storage = "First Storage";
        _storage.PutCreate(req);

        _stream.Close();
        
        var request = new PostDocumentExecuteMailMergeRequest(remoteName, data, BaseTestPath, null, null, null, false, null, null, null, destFileName);
        DocumentResponse actual = _asposewords.PostDocumentExecuteMailMerge(request);

So this all runs with no error. Of course my AppID and SID are actually filled in and not “XXXX”. I also recognize that the “BaseTestPath” is the directory my file is being uploaded to within Aspose Storage. I’ve screenshot my Mail Merge Docx file and the Data File that I’m passing into the “PostDocumentExecuteMailMergeRequest” method.

I’ll add that my code looks a bit different than the code samples given in Aspose’s documentation, because a requirement of mine is that we code against the .Net Core 2.1 framework.

I’ve alsoVersions.PNG (53.4 KB)
attached a screenshot of my currently packages within the project. Might it be an issue with Aspose.Storage-Coud 18.3.0? It seems to upload the document with no problem.

Any help with this is greatly appreciated.

@bdubose

Thank you for contacting Aspose Support.

The API is working perfectly fine on our end. Hopefully, the following code will help you in diagnosing the issue on your end:

// Initialize WordsApi and StorageApi objects
WordsApi WordsApi = new WordsApi(MyAppKey, MyAppSid);
StorageApi StorageApi = new StorageApi(MyAppKey, MyAppSid);

// Input document and Destination file name
var fileName = "SampleMailMergeTemplate.docx";
var destFileName = "TestPostDocumentExecuteMailMerge.docx";

// Upload Input document to Aspose Cloud Storage
PutCreateRequest putCreateRequest = new PutCreateRequest(fileName, File.OpenRead(@"c:\Gists\" + fileName), null, null);
StorageApi.PutCreate(putCreateRequest);

// Mail merge data file
var data = File.ReadAllText(@"c:\Gists\" + "SampleMailMergeTemplateData.txt");

var request = new PostDocumentExecuteMailMergeRequest(fileName, data, null, destFileName: destFileName, withRegions: false);
var actual = WordsApi.PostDocumentExecuteMailMerge(request);

This is what NuGet Packages looks like:

You may use following input documents to test the code on your end:

Input Document: SampleMailMergeTemplate.docx.zip
Mail Merge Data: SampleMailMergeTemplateData.txt.zip

If the issue still persists, please zip your input documents and attach with this thread for investigation on our end.

1 Like

Thank you, that worked.

Where is an example of this code in documentation? I see nothing that matches your example.

1 Like

@bdubose

We are in process of writing SDK Examples of all APIs. The SDK Examples will be hosted on the GitHub:

Do let us know if you want us to prioritize examples of certain APIs.

Thanks.

@bdubose

We would like to update you that since API Version 4.0, we have introduced Storage API methods in Aspsoe.Words Cloud API. Please find sample code to upload a file to cloud storage and you can also check updated .NET SDK with unit tests from our GitHub repository.

var localFileName = "02_pages.docx";
var localFolder = "C:/Temp/";
string remoteFile = "02_pages.docx";

var uploadRequest = new Aspose.Words.Cloud.Sdk.Model.Requests.UploadFileRequest(System.IO.File.OpenRead(localFolder + localFileName), remoteFile);
wordsApi.UploadFile(uploadRequest);