I am trying to automate a MailMerge and have managed to get it almost complete. I can produce the individual docs and merge them. The problem is that during the "InsertFile" call, the document is reformatted, and it ruins the document. Because the document is a letter, and the address information must print in the same spot, I'm stuck until I can correct the formatting. The function I'm using is below. If anyone can point me in the right direction, I would appreciate it. I haven't been able to find anything online in a day of searching.
public bool mailMergeWithTemplate(string strTemplatePath, string strDestinationPath, DataSet dsData, out string strError)
{
strError = "";
object objMissing = Missing.Value; //null value
object objTrue = true;
object objFalse = false;
object objTemplatePath = strTemplatePath;
object objOutputPath = strDestinationPath;
object objOutputPathTemp = strDestinationPath.Substring(0, strDestinationPath.IndexOf(".docx")) + "_temp.docx";
object sectionStart = WdSectionStart.wdSectionNewPage;
Application objWord = null;
try
{
objWord = new Application { Visible = true };
//create an empty document into which we will insert all the merge docs
var objFinalWordDoc = objWord.Documents.Add(ref objMissing, ref objMissing, ref objMissing, ref objMissing);
//for each record in the dataset
var count = 1;
foreach (DataRow dr in dsData.Tables[0].Rows)
{
//insert a document for each record
var objWordDoc = objWord.Documents.Add(ref objTemplatePath, ref objMissing, ref objMissing, ref objMissing);
if (objWordDoc.Fields.Count == 0)
return false;
objWordDoc.Activate();
// Perform mail merge of each field
foreach (Field myMergeField in objWordDoc.Fields)
{
var rngFieldCode = myMergeField.Code;
var strFieldText = rngFieldCode.Text;
// ONLY GET THE MAILMERGE FIELDS
if (!strFieldText.StartsWith(" MERGEFIELD")) continue;
var strFieldName = string.Empty;
if (!strFieldText.Contains("Account"))
{
var intEndMerge = strFieldText.IndexOf("\\");
strFieldName = strFieldText.Substring(11, intEndMerge - 11);
}
else
strFieldName = "Account";
strFieldName = strFieldName.Trim().Replace("\"", "");
//find a matching dataset column
foreach (DataColumn col in dsData.Tables[0].Columns)
{
var strKey = col.ColumnName;
var strValue = dr[strKey].ToString();
if (strFieldName != strKey) continue;
myMergeField.Select();
objWord.Selection.TypeText(strValue);
}
}
//SAVE THE DOCUMENT as temp
objWordDoc.SaveAs(ref objOutputPathTemp, ref objMissing, ref objMissing, ref objMissing,
ref objMissing, ref objMissing, ref objMissing, ref objMissing,
ref objMissing, ref objMissing, ref objMissing, ref objMissing,
ref objMissing, ref objMissing, ref objMissing, ref objMissing);
//CLOSE THE DOCUMENT
objWordDoc.Close(ref objFalse, ref objMissing, ref objMissing);
Marshal.ReleaseComObject(objWordDoc);
objWordDoc = null;
//NOW ADD THE NEW DOC TO THE MAIN DOC
objFinalWordDoc.Activate();
objWord.Selection.InsertFile(objOutputPathTemp.ToString(),
ref objMissing, ref objMissing, ref objMissing, ref objMissing);
if(count < dsData.Tables[0].Rows.Count)
objWord.Selection.InsertBreak(ref sectionStart);
count++;
}
//SAVE THE FINAL DOC
objFinalWordDoc.SaveAs(ref objOutputPath, ref objMissing, ref objMissing, ref objMissing,
ref objMissing, ref objMissing, ref objMissing, ref objMissing,
ref objMissing, ref objMissing, ref objMissing, ref objMissing,
ref objMissing, ref objMissing, ref objMissing, ref objMissing);
//CLOSE THE FINAL DOC
objFinalWordDoc.Close(ref objFalse, ref objMissing, ref objMissing);
Marshal.ReleaseComObject(objFinalWordDoc);
objFinalWordDoc = null;
//now delete the temp file
File.Delete(objOutputPathTemp.ToString());
return true;
}
catch (Exception ex)
{
strError = ex.Message;
}
finally
{
//RELEASE WORD ITSELF
if (objWord != null)
{
objWord.Quit(ref objMissing, ref objMissing, ref objMissing);
Marshal.ReleaseComObject(objWord);
}
objWord = null;
GC.Collect();
}
return false;
}
The issue occurs here:
objWord.Selection.InsertFile(objOutputPathTemp.ToString(),
ref objMissing, ref objMissing, ref objMissing, ref objMissing);
So does anyone have any thoughts as to how I can rectify this issue?
Assuming you have control over the content of the inserted file,