Below is a sniper of code from an application I'm writing in C#. I have added the Microsoft Office Word 11.0 Object Library and the TEMPLATE_FILENAME constant has a valid filename. However the code hangs on the statement
//Open Microsoft Word
Microsoft.Office.Interop.W
ord.Applic
ationClass
wordapp = new Microsoft.Office.Interop.W
ord.Applic
ationClass
();
wordapp.Visible = false;
object filename = TEMPLATE_FILENAME;
object objTrue = true;
object objFalse = false;
object objMiss = Type.Missing;
Microsoft.Office.Interop.W
ord.Docume
nt myMergeDocument;
myMergeDocument = wordapp.Documents.Open(ref
filename, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss);
myMergeDocument.Select();
//Microsoft.Office.Interop
.Word.Mail
Merge mailMerge = myMergeDocument.MailMerge;
// ON THE LINE BELOW: "COMException was unhandled." and "requested object is not available"
myMergeDocument.MailMerge.
Destinatio
n = Microsoft.Office.Interop.W
ord.WdMail
MergeDesti
nation.wdS
endToNewDo
cument;
myMergeDocument.MailMerge.
SuppressBl
ankLines = true;
myMergeDocument.MailMerge.
DataSource
.FirstReco
rd = (int)Microsoft.Office.Inte
rop.Word.W
dMailMerge
DefaultRec
ord.wdDefa
ultFirstRe
cord;
myMergeDocument.MailMerge.
DataSource
.LastRecor
d = (int)Microsoft.Office.Inte
rop.Word.W
dMailMerge
DefaultRec
ord.wdDefa
ultLastRec
ord;
myMergeDocument.MailMerge.
Execute(re
f objFalse);
//myMergeDocument.Close();
wordapp.Visible = true;
wordapp.ShowMe();
return;
--------------
I have a word document that is set up with a mail merge source already and I just want to make it open and perform the merge. The merge source is a text file. The Open command doesn't seem to work correctly because the mail merge toolbar is disabled. Someone please help!!!
(I know how to use try catch blocks - i'm interested in fixing the problem...not just handling it gracely)