Hello,
I’m attempting to run a simple merge into an existing document that contains merge fields in Shapes. The data source is a DataTable and appears to run when the table has a single row, but when the table contains multiple rows, an exception is thrown on the document.Save(fileStream, SaveFormat.Docx)( Object reference not set to an instance of an object.); step. Any help would be appreciated. I have provided some code below. Seems pretty straight forward.
string dataDir = @"C:\InstaMerge_Uploads\";
// Open an existing document.
Document document = new Document(dataDir + "MyMerge.docx");
DataTable myData = new DataTable("MyData");
DataColumn column = new DataColumn();
column = new DataColumn();
column.ColumnName = "donor_id";
myData.Columns.Add(column);
column = new DataColumn();
column.ColumnName = "prof_title";
myData.Columns.Add(column);
column = new DataColumn();
column.ColumnName = "address";
myData.Columns.Add(column);
DataRow row;
row = myData.NewRow();
row["donor_id"] = "34";
row["prof_title"] = "CEO";
row["address"] = "65 Ember Lane";
myData.Rows.Add(row);
row = myData.NewRow();
row["donor_id"] = "88";
row["prof_title"] = "CFO";
row["address"] = "132 Welsh Lane";
myData.Rows.Add(row);
document.MailMerge.Execute(myData);
MemoryStream fileStream = new MemoryStream();
document.Save(fileStream, SaveFormat.Docx);