Click here to Skip to main content
16,012,468 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
                }
                // Load Word if it's not.
                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>
Posted
Updated 15-Nov-10 0:22am
v2

1 solution

the description and code is here:
http://www.codeproject.com/KB/office/word_application.aspx[^]

who can help me find the problem?thank you!
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900