谁可以帮助我打开和关闭Word.Document? [英] Who can help me about Opening and closing the Word.Document?

查看:311
本文介绍了谁可以帮助我打开和关闭Word.Document?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我开发的应用程序中,我经常使用Microsoft Word生成各种文档页面.当用户打开* .doc文件时,将添加一个带有WinWordControl的页面,然后控件"WinWordControl"将加载* .doc文件,它将检查如果Word辅助类已经创建并在运行对象表中注册.如果不是,它将创建Word.Application协同类的新实例,该实例将加载WinWord应用程序.代码为:

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;
			}
		}




问题是当我打开一个word文档时,关闭页面,调用CloseControl()将花费几秒钟,打开的文档将被关闭,但

当我打开另一个* .doc(通常关闭并打开几次)时,应用程序将获取已创建的Word.ApplicationClass,然后调用wd.Documents.Open(引用文件名,引用丢失,
ref readOnly,ref缺失,ref缺失,ref缺失,
裁判缺失,裁判缺失,裁判缺失,裁判缺失,
ref缺失,ref isVisible);
wd.Visible = true;
wd.Activate();
这会导致问题(尽管我调用了CloseControl(),但是我发现wd.Documents.Count的数量不会减少.新窗口无法打开),

怎么了?谁能告诉我?谢谢!

note:the files *.doc should put in the D:\\<a href=""></a>




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>

推荐答案

描述和代码在这里:
http://www.codeproject.com/KB/office/word_application.aspx [ ^ ]

谁能帮助我找到问题?谢谢!
the description and code is here:
http://www.codeproject.com/KB/office/word_application.aspx[^]

who can help me find the problem?thank you!


这篇关于谁可以帮助我打开和关闭Word.Document?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆