How to automate Word with Visual Basic to create a Mail Merge Sample Link 1
How to automate Microsoft Word to perform Mail Merge from Visual C# Sample Link 2
The best Sample code which I like is available here with step by step: Sample Link 3
Sample Code I used for my application. My requirement was to send an eFax with some text body which will load from another doc file.
Step1: Created a word template file with tow merge fields ('Header' and 'Footer') and a bookmark with name 'DocumentContents'.
Step2: Open the template file and set the header and footer
Step3: Place the source file contents to the bookmark
Step4: Save the modified template file as output file
Code:
public void Merge(string templateFile, string sourceFile, string destFile)
{
if(!File.Exists(templateFile))
{
throw new FileNotFoundException(string.Format("Template file '{0}' does not exist",templateFile));
}
if (!File.Exists(sourceFile))
{
throw new FileNotFoundException(string.Format("Source file '{0}' does not exist", sourceFile));
}
if (File.Exists(destFile))
{
File.Delete(destFile);
}
object oFalse = false;
object oMissing = Missing.Value;
Application oWord = new Application();
Document oWordDoc = new Document();
try
{
oWord.Visible = true;
object oTemplatePath = templateFile;
oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
foreach (Field myMergeField in oWordDoc.Fields)
{
Microsoft.Office.Interop.Word.Range rngFieldCode = myMergeField.Code;
String fieldText = rngFieldCode.Text;
// ONLY GETTING THE MAILMERGE FIELDS
if (fieldText.StartsWith(" MERGEFIELD"))
{
// THE TEXT COMES IN THE FORMAT OF
// MERGEFIELD MyFieldName \\* MERGEFORMAT
Int32 endMerge = fieldText.IndexOf("\\");
Int32 fieldNameLength = fieldText.Length - endMerge;
String fieldName = fieldText.Substring(11, endMerge - 11).Trim();
if (fieldName == "Header")
{
myMergeField.Select();
oWord.Selection.TypeText("Set Header of the File");
}
if (fieldName == "Footer")
{
myMergeField.Select();
oWord.Selection.TypeText("Set Footer of the File");
}
}
}
object oBookMark = "DocumentContents";
String oFilePath = sourceFile;
oWordDoc.Bookmarks.get_Item(ref oBookMark).Range.InsertFile(oFilePath, ref oMissing, ref oFalse, ref oFalse, ref oFalse);
Object oSaveAsFile = (Object)destFile;
oWordDoc.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
}
finally
{
oWordDoc.Close(ref oFalse, ref oMissing, ref oMissing);
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
Marshal.ReleaseComObject(oWordDoc);
}
}
public void Merge(string templateFile, string sourceFile, string destFile)
{
if(!File.Exists(templateFile))
{
throw new FileNotFoundException(string.Format("Template file '{0}' does not exist",templateFile));
}
if (!File.Exists(sourceFile))
{
throw new FileNotFoundException(string.Format("Source file '{0}' does not exist", sourceFile));
}
if (File.Exists(destFile))
{
File.Delete(destFile);
}
object oFalse = false;
object oMissing = Missing.Value;
Application oWord = new Application();
Document oWordDoc = new Document();
try
{
oWord.Visible = true;
object oTemplatePath = templateFile;
oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
foreach (Field myMergeField in oWordDoc.Fields)
{
Microsoft.Office.Interop.Word.Range rngFieldCode = myMergeField.Code;
String fieldText = rngFieldCode.Text;
// ONLY GETTING THE MAILMERGE FIELDS
if (fieldText.StartsWith(" MERGEFIELD"))
{
// THE TEXT COMES IN THE FORMAT OF
// MERGEFIELD MyFieldName \\* MERGEFORMAT
Int32 endMerge = fieldText.IndexOf("\\");
Int32 fieldNameLength = fieldText.Length - endMerge;
String fieldName = fieldText.Substring(11, endMerge - 11).Trim();
if (fieldName == "Header")
{
myMergeField.Select();
oWord.Selection.TypeText("Set Header of the File");
}
if (fieldName == "Footer")
{
myMergeField.Select();
oWord.Selection.TypeText("Set Footer of the File");
}
}
}
object oBookMark = "DocumentContents";
String oFilePath = sourceFile;
oWordDoc.Bookmarks.get_Item(ref oBookMark).Range.InsertFile(oFilePath, ref oMissing, ref oFalse, ref oFalse, ref oFalse);
Object oSaveAsFile = (Object)destFile;
oWordDoc.SaveAs(ref oSaveAsFile, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
}
finally
{
oWordDoc.Close(ref oFalse, ref oMissing, ref oMissing);
oWord.Quit(ref oMissing, ref oMissing, ref oMissing);
Marshal.ReleaseComObject(oWordDoc);
}
}
No comments:
Post a Comment