问题描述
这些是我创建文档的代码:
_WordApp = new Interop.Word.Application();
_WordApp.Visible = false;
_WordDoc = new Document();
_Missing = System.Reflection.Missing.Value;
_WordDoc = _WordApp.Documents.Add(ref _Filename,ref _Missing,ref _Missing,
ref _Missing);
问题我它是否会创建另一个Word实例。
如果我已经运行了Word,它将运行另一个Word。
如何检测是否有另一个Word正在运行,以便我可以将该实例分配给_WordApp?
唯一好的解决方案是使用
Marshal.GetActiveObject调查运行对象表(ROT)。 br />
尝试
{
// Word是否正在运行?
_WordApp = Marshal.GetActiveObject(" Word.Application")作为ApplicationClass;
}
catch(COMException ce)
{
if(ce.ErrorCode == unchecked((int)0x800401E3))
//不,Word不在ROT中,启动一个新实例
_WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
}
//使用_WordApp引用的实例..
...
Willy。
These are the codes I created a document:
_WordApp = new Interop.Word.Application();
_WordApp.Visible = false;
_WordDoc = new Document();
_Missing = System.Reflection.Missing.Value;
_WordDoc = _WordApp.Documents.Add(ref _Filename, ref _Missing, ref _Missing,
ref _Missing);
The problem I have is it will create another instance of Word.
If I have already a Word is running, it will have another Word running.
How do I detect if there is another Word is running so that I can assign
that instance to _WordApp?
Only good solution is to investigate the Running Object Table (ROT) using
Marshal.GetActiveObject.
try
{
// Is Word running?
_WordApp = Marshal.GetActiveObject("Word.Application") as ApplicationClass;
}
catch (COMException ce)
{
if(ce.ErrorCode == unchecked((int)0x800401E3))
// No, Word not in ROT, start a new instance
_WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
}
// Use instance referened by _WordApp ..
...
Willy.
这篇关于Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!