In applications I develop, I often use Microsoft Word to generate various document pages.When the user open a *.doc, a page with WinWordControl will be added,Tne control "WinWordControl" loade the *.doc,it will checks if the Word co-class is already created and registered in the running object table. If it's not, it creates a new instance of the Word.Application co-class which loads the WinWord application.the code is:
try
{
wd = (Word.ApplicationClass)Marshal.
GetActiveObject("Word.Application");
}
catch
{
wd = null;
}
if (wd == null)
{
try
{
wd = new Word.ApplicationClass();
wd.CommandBars.AdaptiveMenus = false;
wd.NewDocument += new Word.ApplicationEvents2_NewDocumentEventHandler(OnNewDoc);
wd.DocumentOpen += new Word.ApplicationEvents2_DocumentOpenEventHandler(OnOpenDoc);
}
catch
{
wd = null;
return;
}
}
then open the document,the code is :
document = wd.Documents.Open(ref fileName, ref missing,
ref readOnly, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref isVisible);
when the page is shut down,the application will close the document,the function CloseControl() is :
public void CloseControl()
{
try
{
deactivateevents = true;
object dummy=null;
document.Close(ref dummy, ref dummy, ref dummy);
deactivateevents = false;
}
catch(Exception ex)
{
String strErr = ex.Message;
}
}
the question is when i opened a word document, close the page,it will take several seconds call the CloseControl(),the document opened will be closed but
when i open another *.doc(close and open several times usually),the application get the Word.ApplicationClass already created,and call wd.Documents.Open(ref fileName, ref missing,
ref readOnly, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref isVisible);
wd.Visible = true;
wd.Activate();
it will cause a problem(though i call the CloseControl(),but i found the number of wd.Documents.Count will not less.the new window can not open),
what's wrong?who can tell me?thank you!!
note:the files *.doc should put in the D:\\<a href=""></a>