我正在部署使用 C# 在 Visual Studio 2008 中创建的 Outlook 2007 加载项。我创建了一个从 word 文档中提取文本的函数。
问题是大约 20% 的时间,这会暂时抓住焦点并将其交给我的插件。当我的加载项运行时,我坐下来观看了 sysinternal 的进程资源管理器大约 10 分钟,并且只有当 word 进程运行超过 2 秒时才会发生这种情况。WINWORD 进程关闭后 - 焦点将恢复到之前的任何程序。
有谁知道为什么 WINWORD 进程会代表我的加载项窃取焦点以及如何避免它?
public static string ExtractWordDocument(object filename)
{
Word.Application wordApp = new Word.Application();
string content = null;
Word.Document doc = null;
try
{
doc = wordApp.Documents.OpenNoRepairDialog(
ref filename,
ref officeObjects.Negative,
ref officeObjects.ReadOnly,
ref officeObjects.Negative,
ref officeObjects.Nullobj,
ref officeObjects.Nullobj,
ref officeObjects.Nullobj,
ref officeObjects.Nullobj,
ref officeObjects.Nullobj,
ref officeObjects.Nullobj,
ref officeObjects.Nullobj,
ref officeObjects.Negative,
ref officeObjects.Negative,
ref officeObjects.Nullobj,
ref officeObjects.Nullobj,
ref officeObjects.Nullobj);
content = doc.Content.Text;
}
catch (Exception exc)
{
Util.Log("Error processing word document - Exception received: " + exc.ToString());
}
if (doc != null)
{
((Word._Document)doc).Close(ref officeObjects.SaveChanges, ref officeObjects.OriginalFormat, ref officeObjects.RouteDocument);
}
((Word._Application)wordApp).Quit(ref officeObjects.SaveChanges, ref officeObjects.OriginalFormat, ref officeObjects.RouteDocument);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(wordApp);
doc = null;
wordApp = null;
return content;
}