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

Free Support Forum - aspose.com

Aspose Words empty includetext

I have run into a problem when performing a mail merge with fields like:

{IF “{MERGEFIELD 426}” <> “” “{INCLUDETEXT “{MERGEFIELD 426}” }” “”}
These are used to conditionally include documents from an existing legacy system which generates the merge data.

What happens is if they do not want to include the document an empty string is sent through in the merge field value. If this is the case aspose words mailmerge (doc.MailMerge.Execute) throws: “URIFormatException: Invalid URI: The URI is Empty”. Is there any way to get around this?

Hi James,

Thank you for inquiry. Unfortunately, it is difficult for us to say what is the problem. Could you please share the input file along with code snippet.

We’ll guide you accordingly.

Attaced are both an example datafile and templatefile. The datafile is a tab seperated list, 1st line fieldnames second line has the corresponding fiels values. In this example fields "1" and "2" are document paths, but have been left empty as the system does not want to include those in the output file. I have included an example c# codelisting below.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using Aspose.Words;
using System.Text.RegularExpressions;
using Aspose.Words.Fields;
using System.IO;
using System.Collections;
using Aspose.Words.Reporting;

namespace AsposeTest
class Program
static void Main(string[] args)

Aspose.Words.License license = new Aspose.Words.License();

string dataFile;
string templateFile;
string outfile;
if (args.Length != 3)
Console.WriteLine("Incorrect Format\r\nPlease run as " + System.AppDomain.CurrentDomain.FriendlyName + " ");

dataFile = args[0];
templateFile = args[1];
outfile = args[2];

StreamReader fs = new StreamReader(File.Open(dataFile, FileMode.Open));
string[] fields = (fs.ReadLine()).Split(new string[] { "\t" }, StringSplitOptions.None);
string[] values = (fs.ReadLine()).Split(new string[] { "\t" }, StringSplitOptions.None);

Document generatedDoc = new Document();

Document doc = new Aspose.Words.Document(templateFile);

doc.MailMergeSettings.CheckErrors = Aspose.Words.Settings.MailMergeCheckErrors.CollectErrors;
doc.MailMergeSettings.DoNotSupressBlankLines = false;

doc.MailMerge.RemoveEmptyParagraphs = true;
doc.MailMerge.RemoveEmptyRegions = true;
doc.MailMerge.UseNonMergeFields = false;

doc.MailMerge.Execute(fields, values);
catch (Exception ex)
Console.WriteLine("Error merging " + templateFile + ": " + ex.ToString());

generatedDoc.AppendDocument(doc, ImportFormatMode.KeepSourceFormatting);



Hi James,

Thank you for details. You can avoid exception by including default document string as following:

Document doc = new Aspose.Words.Document("c:/temp/Doc1.doc");

// Specify field merging callback.

doc.MailMerge.FieldMergingCallback = new FieldMergingCallback(doc);

doc.MailMerge.Execute(fields, values);

public class FieldMergingCallback : IFieldMergingCallback


public void FieldMerging(FieldMergingArgs args)


if (args. FieldValue == "")


args.Text = "c:/temp/Empty.doc";



public void ImageFieldMerging(ImageFieldMergingArgs args)


// Do nothing here.



For more details, please visit documentation here. I hope this will help.

Hi Imran,

Thanks for the reply. It works, but unfortunately there are other mergefields that may be blank in the document that are merely displayed. Using the above solution prints the string path in the merged document for each of these blank fields. I need to specifically targer only the mergefields within includetext fields

Hi James,

Thanks for the additional information.

In that case, you can wrap your merge field in an IF field based off this condition which would hide the path string in the output but not remove it. Please modify your template as below:

{ IF "{MERGEFIELD 1}" = "C:/TEMP/Empty.doc" "" "{MERGEFIELD 1}" }

I hope this will help.