Word ApplicationClass问题 [英] Word ApplicationClass issue
问题描述
我正在使用Microsoft.Office.Interop.Word.ApplicationClass
读取一组Word文档.我可以正常阅读所有内容,但是我注意到根据Windows任务管理器,用于读取这些文档的过程实际上从未结束.
I am using Microsoft.Office.Interop.Word.ApplicationClass
to read a set of Word documents. I am able to read them fine and all, but I noticed that the process that is used to read these docs never actually ends according to the Windows Task Manager.
从我对Google的了解来看,似乎没有其他人遇到这个问题,这使我相信我在做根本上是错误的事情,或者我缺乏有效地表达我的观点的能力Google搜索.
From what I have Google'd, there doesn't seem to be anyone else who has this issues, which leads me to believe that I either am doing something fundamentally wrong, or I lack the ability to effectively phrase my issue for a Google search.
我对C#比较陌生,所以我怀疑是前者.在下面找到我用来创建文档阅读器"实例的代码.
I am relatively new with C#, so I suspect the former. Find below the code that I am using to create the instance of the document 'reader'.
private void readDoc(string docPath)
{
Word.ApplicationClass wordApp = new Word.ApplicationClass();
object nullObj = System.Reflection.Missing.Value;
object roObj = true;
object objFile = docPath;
try
{
Word.Document doc = wordApp.Documents.Open(ref objFile,
ref nullObj, ref roObj, ref nullObj, ref nullObj, ref nullObj,
ref nullObj, ref nullObj, ref nullObj, ref nullObj, ref nullObj,
ref nullObj, ref nullObj, ref nullObj, ref nullObj, ref nullObj);
doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy();
IDataObject tmpData = Clipboard.GetDataObject();
string docText = tmpData.GetData(DataFormats.Text).ToString();
(...)
}
catch (Exception ex)
{
MessageBox.Show(ex.Data);
}
}
try块的其余部分处理字符串docText
,并且不涉及wordApp
或doc
,因此我没有将其包括在代码段中.
The rest of the try block deals with the string docText
and doesn't involve wordApp
or doc
, thus I have not included it in the code segment.
Word.ApplicationClass
似乎没有.Dispose()函数,所以我在这里有点茫然.
There doesn't seem to be a .Dispose() function for the Word.ApplicationClass
so I am at a bit of a loss here.
编辑-抱歉,这里隐含的问题是:我如何以编程方式结束该过程?
Edit- Sorry, the implied question here is: How can I end the process programmatically?
推荐答案
尝试在readDoc()
的末尾添加以下内容:
Try adding the following at the end of readDoc()
:
wordApp.Quit(ref nullObj, ref nullObj, ref nullObj);
这篇关于Word ApplicationClass问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!