Word文档 [英] Word document

查看:71
本文介绍了Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些是我创建文档的代码:


_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?

解决方案

这是我用Excel做的方式。抱歉;这是在VB中。


这是一个GetObject,它获取对当前实例

Excel的引用,如果它正在运行。如果失败了,那么我尝试使用CreateObject来创建一个

的新实例。


尝试

XlApp = CType(GetObject(, " Excel.Application"),Excel.Application)

AppExistsFlag =" Exists"

Catch ex As Exception

试试

XlApp = CType(CreateObject(" Excel.Application")),Excel.Application)

Catch ex2 As Exception

MsgBox(必须有Excel)安装。)

AppExistsFlag = String.Empty

结束尝试

结束尝试


Robin S.

--------------------------------------- -----


" Alan T" < al ************* @ yahoo.com.auwrote in message

news:%2 ************** ** @ TK2MSFTNGP06.phx.gbl ...


这些是我创建文档的代码:


_WordApp =新的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?



为了检测是否存在一个或多个Word会话,请使用

带有cla的FindWindow API调用是OpusApp的名称(这是

所有版本的Word的类名)。此API返回它找到的第一个窗口....

可能有几个Word实例。这很麻烦;你不能使用

windows手柄来获取实例。


请尝试以下方法:


Word.Document wd = new Word.Document();

Word.Application wa = wd.Application;

wd = null;


ie


1.创建Word.Document实例的实例:如果有一个实例

的Word,则会在其中创建一个新文档。如果有多个Word实例,你仍然不知道你正在使用哪个实例




2.创建一个Word实例。来自Word.Document实例的应用程序。


3.删除Word.Document&的实例。使用Word.Applicaiton

对象。


注意:


a。如果你已经抓住了一个现有实例,那么该实例可能已经可以看到了


b。如果没有现有实例,您将创建一个新实例

并且它将不可见。


" Alan T < al ************* @ yahoo.com.auwrote in message

news:%2 ************** ** @ TK2MSFTNGP06.phx.gbl ...


这些是我创建文档的代码:


_WordApp =新的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?

解决方案

Here''s how I do it with Excel. Sorry; this is in VB.

This does a GetObject which gets a reference to the current instance of
Excel if it''s running. If that fails, then I try CreateObject to create a
new instance.

Try
XlApp = CType(GetObject(, "Excel.Application"), Excel.Application)
AppExistsFlag = "Exists"
Catch ex As Exception
Try
XlApp = CType(CreateObject("Excel.Application"), Excel.Application)
Catch ex2 As Exception
MsgBox("Must have Excel installed.")
AppExistsFlag = String.Empty
End Try
End Try

Robin S.
--------------------------------------------

"Alan T" <al*************@yahoo.com.auwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...

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?



In order to detect whether one or more sessions of Word exists, use the
FindWindow API call with a class name of OpusApp (this is the class name for
all versions of Word). This API returns the first window that it finds ....
there may be several instances of Word. This is messy; you cannot use the
windows handle to grab the instance.

Try the following:

Word.Document wd = new Word.Document();
Word.Application wa = wd.Application;
wd = null;

i.e.

1. Create an instance of the Word.Document instance: if there is an instance
of Word, a new document is created in it. You still won''t know which instance
of Word you are using if there are several instances of Word.

2. Create an instance of Word.Application from the instance of Word.Document.

3. Remove the instance of the Word.Document & work with the Word.Applicaiton
object.

Notes:

a. If you have grabbed an existing instance, that instance may already be
visible.
b. If there were no existing instances, you will have create a new instance
and it will NOT be visible.


"Alan T" <al*************@yahoo.com.auwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...

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文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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