For some reason my word mail merge throws execption when it reaches
wrdMailMerge = oDataDoc.MailMerge.
Can any body guide me in a clean wasy of doing mailerge from sql server 2005
table ? I really appreciate your help. Below is the code I tried hundred
times but throws exception.
try
{
Word.Application wrdApp;
Word.Document oDataDoc;
Word.MailMerge wrdMailMerge;
Object oMissing = System.Type.Missing;
Object oFalse = false;
Object oTrue = true;
Object oName = "c:\\templatefile.dot";
Object oFileName = "c"\\outputfile.doc";
Object oFileFormat = Word.WdSaveFormat.wdFormatRTF;
Object oConnection = "Data Source=sand01;Catalog=mydatabase;Integrated
Security=SSPI";;
Object oQuery = "select * from mytable" ;
Object oSubType = Word.WdMergeSubType.wdMergeSubTypeWord;
wrdApp = new Word.ApplicationClass();
wrdApp.Visible = true;
wrdApp.WindowState = Word.WdWindowState.wdWindowStateMaximize;
oDataDoc = wrdApp.Documents.Add(ref oName, ref oFalse, ref oMissing, ref
oMissing);
oDataDoc.Activate();
wrdMailMerge = oDataDoc.MailMerge;
wrdMailMerge.HighlightMergeFields = false;
wrdMailMerge.ViewMailMergeFieldCodes = 0;
wrdMailMerge.OpenDataSource("", ref oFileFormat, ref oMissing, ref oMissing,
ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oFalse, ref
oMissing, ref oMissing, ref oConnection, ref oQuery, ref oMissing, ref
oMissing, ref oSubType);
wrdMailMerge.SuppressBlankLines = true;
oDataDoc.SaveAs(ref oFileName, ref oFileFormat, ref oMissing, ref oMissing,
ref oTrue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref
oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref
oMissing, ref oMissing);
oDataDoc.Saved = true;
// Unload objects from the memory
wrdMailMerge = null;
oDataDoc = null;
wrdApp = null;
}
catch (Exception exc)
{
}