save merged document from c# without opening word
Hi,
I would like to save a merged document to disk, without opening word.
The merging works fine if I make word visible and let the user save the doc,
but if I try to save from code without showing word, i get 'Opening this
document will run the following SQL command'.
The code I use is as follows :
object objNull = System.Reflection.Missing.Value;
string strDataSource = @"C:\HRAccent\MailMerge\dataEmpl.csv";
object strTemplatePath = @"C:\HRAccent\MailMerge\testTemplate.doc";
object objTrue = true;
object objFalse = false;
object objOpenFormat;
objOpenFormat = Microsoft.Office.Interop.Word.WdOpenFormat.wdOpenF ormatAuto;
Microsoft.Office.Interop.Word.Application wrdApp = new
Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word._Document wrdDoc;
Microsoft.Office.Interop.Word.MailMerge wrdMailMerge;
wrdDoc = wrdApp.Documents.Open(ref strTemplatePath, ref objFalse, ref
objTrue, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref
objNull, ref objNull, ref objNull, ref objFalse, ref objNull, ref objNull,
ref objNull);
wrdApp.Visible = false;
wrdDoc.Activate();
wrdDoc.MailMerge.OpenDataSource(strDataSource, ref objNull, ref objFalse,
ref objTrue, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull,
ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull,
ref objNull);
wrdMailMerge = wrdDoc.MailMerge;
wrdMailMerge.Destination =
Microsoft.Office.Interop.Word.WdMailMergeDestinati on.wdSendToNewDocument;
wrdMailMerge.Execute(ref objFalse);
object wdFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdForma tRTF;
Object fileName = @"C:\HRAccent\MailMerge\test.doc";
wrdDoc.SaveAs( ref fileName, ref objNull, ref objNull, ref objNull, ref
objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref
objNull, ref objNull, ref objNull, ref objNull, ref objNull, ref objNull);
wrdDoc.Saved = true;
wrdDoc.Close(ref objTrue,ref objNull,ref objNull);
wrdApp.Quit(ref objFalse,ref objNull,ref objNull);
wrdMailMerge = null;
wrdDoc = null;
wrdApp = null;
Anyone any ideas?
Thanx,
t
|