469,299 Members | 2,079 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,299 developers. It's quick & easy.

Word document

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?
Feb 27 '07 #1
14 4984
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?


Feb 27 '07 #2
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.

Feb 27 '07 #3
"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.

Feb 27 '07 #4
Hi Willy, I learnt something with

_WordApp = Marshal.GetActiveObject("Word.Application") as ApplicationClass;

I have 2 questions:

1. What is the purpose of _ in _WordApp and why is there no type preceding it?

2. With this line
if(ce.ErrorCode == unchecked((int)0x800401E3))
if it turns out to be false, would you not end up with an uninitialised
variable _WordApp?
Feb 27 '07 #5
"AA2e72E" <AA*****@discussions.microsoft.comwrote in message
news:98**********************************@microsof t.com...
Hi Willy, I learnt something with

_WordApp = Marshal.GetActiveObject("Word.Application") as ApplicationClass;

I have 2 questions:

1. What is the purpose of _ in _WordApp and why is there no type preceding it?

2. With this line
if(ce.ErrorCode == unchecked((int)0x800401E3))
if it turns out to be false, would you not end up with an uninitialised
variable _WordApp?

1) _WordApp is what the OP used as a variable for it's Word.Application reference in his
(incomplete)sample.
2) No, COM HRESULT = 0x800401E3, means that there is no running instance, so I have create
one and set the variable to the instance, like this:

if(ce.ErrorCode == unchecked((int)0x800401E3))
_WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Willy.
Feb 27 '07 #6
Thanks for your reply.

This is my declaration:

private Interop.Word.Application _WordApp;

Is there any differences between Application and ApplicationClass?

I don't have Mirosoft.Office.Interop.Word.ApplicationClass();

but just have:

_WordApp = new Interop.Word.Application();

Any differences?

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:ez**************@TK2MSFTNGP04.phx.gbl...
"AA2e72E" <AA*****@discussions.microsoft.comwrote in message
news:98**********************************@microsof t.com...
>Hi Willy, I learnt something with

_WordApp = Marshal.GetActiveObject("Word.Application") as
ApplicationClass;

I have 2 questions:

1. What is the purpose of _ in _WordApp and why is there no type
preceding it?

2. With this line
if(ce.ErrorCode == unchecked((int)0x800401E3))
if it turns out to be false, would you not end up with an uninitialised
variable _WordApp?


1) _WordApp is what the OP used as a variable for it's Word.Application
reference in his (incomplete)sample.
2) No, COM HRESULT = 0x800401E3, means that there is no running instance,
so I have create one and set the variable to the instance, like this:

if(ce.ErrorCode == unchecked((int)0x800401E3))
_WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
Willy.


Feb 28 '07 #7
"Alan T" <al*************@yahoo.com.auwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
Thanks for your reply.

This is my declaration:

private Interop.Word.Application _WordApp;

Is there any differences between Application and ApplicationClass?

I don't have Mirosoft.Office.Interop.Word.ApplicationClass();

but just have:

_WordApp = new Interop.Word.Application();

Any differences?

No differences, they are all namespaces as generated by the typelib importer,
Mirosoft.Office.Interop.Word is the namespace you get when adding a reference to Word 2007,
and follows the namespace naming guidelines as applied by the importer
(CompanyName.TechnologyName.xxx).
Interop.Word is the namespace you got by ???

As for the Class suffix used, this is the normal way to instantiate a coclass.
Granted you can use Application as type (which is an Interface), however, as C# cannot
instantiate interfaces, it requires some extra work from the C# compiler to search the
metadata for a CoClassAttribute (generated by the tlbimp utility) and swap the interface
(Application) with the tpe sored in this CoClassAttribute (ApplicationClass) at compile
time. You can prevent this action by using the Class suffix. Note that C++ doesn't perform
this extra work so there you have to specify the suffix, also when creating instances by
anything other than "new", you also have to specify the suffix.
In one word, use the suffix all the time.

Willy.


Feb 28 '07 #8
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:ej**************@TK2MSFTNGP02.phx.gbl...
"Alan T" <al*************@yahoo.com.auwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
>Thanks for your reply.

This is my declaration:

private Interop.Word.Application _WordApp;

Is there any differences between Application and ApplicationClass?

I don't have Mirosoft.Office.Interop.Word.ApplicationClass();

but just have:

_WordApp = new Interop.Word.Application();

Any differences?


No differences, they are all namespaces as generated by the typelib importer,
Mirosoft.Office.Interop.Word is the namespace you get when adding a reference to Word
2007, and follows the namespace naming guidelines as applied by the importer
(CompanyName.TechnologyName.xxx).
Interop.Word is the namespace you got by ???

As for the Class suffix used, this is the normal way to instantiate a coclass.
Granted you can use Application as type (which is an Interface), however, as C# cannot
instantiate interfaces, it requires some extra work from the C# compiler to search the
metadata for a CoClassAttribute (generated by the tlbimp utility) and swap the interface
(Application) with the tpe sored in this CoClassAttribute (ApplicationClass) at compile
time. You can prevent this action by using the Class suffix. Note that C++ doesn't perform
this extra work so there you have to specify the suffix, also when creating instances by
anything other than "new", you also have to specify the suffix.
In one word, use the suffix all the time.

Willy.
Some typo corrections,

with the tpe sored in this CoClassAttribute
should read...
with the type stored in this CoClassAttribute

Willy.

Feb 28 '07 #9
Thanks for your explanation.
I got a problem:
In a case when a Word is already opening, then use your code to create a
reference of this Word:

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

However, when it exits the code or my class, the first already running Word
application and its documents will be closed.
How do I keep the first running Word application running ?

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:uE*************@TK2MSFTNGP06.phx.gbl...
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:ej**************@TK2MSFTNGP02.phx.gbl...
>"Alan T" <al*************@yahoo.com.auwrote in message
news:%2***************@TK2MSFTNGP06.phx.gbl...
>>Thanks for your reply.

This is my declaration:

private Interop.Word.Application _WordApp;

Is there any differences between Application and ApplicationClass?

I don't have Mirosoft.Office.Interop.Word.ApplicationClass();

but just have:

_WordApp = new Interop.Word.Application();

Any differences?


No differences, they are all namespaces as generated by the typelib
importer,
Mirosoft.Office.Interop.Word is the namespace you get when adding a
reference to Word 2007, and follows the namespace naming guidelines as
applied by the importer (CompanyName.TechnologyName.xxx).
Interop.Word is the namespace you got by ???

As for the Class suffix used, this is the normal way to instantiate a
coclass.
Granted you can use Application as type (which is an Interface), however,
as C# cannot instantiate interfaces, it requires some extra work from the
C# compiler to search the metadata for a CoClassAttribute (generated by
the tlbimp utility) and swap the interface (Application) with the tpe
sored in this CoClassAttribute (ApplicationClass) at compile time. You
can prevent this action by using the Class suffix. Note that C++ doesn't
perform this extra work so there you have to specify the suffix, also
when creating instances by anything other than "new", you also have to
specify the suffix.
In one word, use the suffix all the time.

Willy.

Some typo corrections,

with the tpe sored in this CoClassAttribute
should read...
with the type stored in this CoClassAttribute

Willy.


Feb 28 '07 #10
"Alan T" <al*************@yahoo.com.auwrote in message
news:eU**************@TK2MSFTNGP03.phx.gbl...
Thanks for your explanation.
I got a problem:
In a case when a Word is already opening, then use your code to create a reference of this
Word:

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

However, when it exits the code or my class, the first already running Word application
and its documents will be closed.
How do I keep the first running Word application running ?
This has nothing to do with above code, the running instance will stay running unless you
are calling ApplicationClass Quit method.
Anyway, when using COM automation, it's the client who determines the lifetime of the server
application (here Word), and it's normal that the client survives the "Server" application,
that is, the client needs to end the server's activities, say close/save work done and call
Quit.
Why do you want to keep Word running?

Willy.

Feb 28 '07 #11
Hi,

My case is on a machine, the user may have opened a Word document for
editing.
In sometime, he opens my application, this application contains my code.
Then he closed my application, continue to edit his already opened Word
document.

My problem is when he closed my application, the already opened Word and
document are closed.
Even I didn't call word.application.quit(....).

Any idea?
"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
"Alan T" <al*************@yahoo.com.auwrote in message
news:eU**************@TK2MSFTNGP03.phx.gbl...
>Thanks for your explanation.
I got a problem:
In a case when a Word is already opening, then use your code to create a
reference of this Word:

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

However, when it exits the code or my class, the first already running
Word application and its documents will be closed.
How do I keep the first running Word application running ?

This has nothing to do with above code, the running instance will stay
running unless you are calling ApplicationClass Quit method.
Anyway, when using COM automation, it's the client who determines the
lifetime of the server application (here Word), and it's normal that the
client survives the "Server" application, that is, the client needs to end
the server's activities, say close/save work done and call Quit.
Why do you want to keep Word running?

Willy.

Feb 28 '07 #12
"Alan T" <al*************@yahoo.com.auwrote in message
news:eA**************@TK2MSFTNGP05.phx.gbl...
Hi,

My case is on a machine, the user may have opened a Word document for editing.
Right.
In sometime, he opens my application, this application contains my code.
Then he closed my application, continue to edit his already opened Word document.
Your application has to save the work done, but shouldn't call _Application.Quit(....);
My problem is when he closed my application, the already opened Word and document are
closed.
Should not happen if the application doesn't call Quit.
Even I didn't call word.application.quit(....).
Should work, at least it works for me, not calling Quit keeps Word running (as it should).

Willy.

Mar 1 '07 #13
Hi,

If I don't call Quit, it will raise a problem:
In a scenario no Word is running, the user runs my application only and
finishes my application, and then there will be a WinWord.exe instance in
the Task Manager after quitting my application.

"Willy Denoyette [MVP]" <wi*************@telenet.bewrote in message
news:Oa**************@TK2MSFTNGP03.phx.gbl...
"Alan T" <al*************@yahoo.com.auwrote in message
news:eA**************@TK2MSFTNGP05.phx.gbl...
>Hi,

My case is on a machine, the user may have opened a Word document for
editing.

Right.
>In sometime, he opens my application, this application contains my code.
Then he closed my application, continue to edit his already opened Word
document.

Your application has to save the work done, but shouldn't call
_Application.Quit(....);
>My problem is when he closed my application, the already opened Word and
document are closed.

Should not happen if the application doesn't call Quit.
>Even I didn't call word.application.quit(....).

Should work, at least it works for me, not calling Quit keeps Word running
(as it should).

Willy.

Mar 2 '07 #14
"Alan T" <al*************@yahoo.com.auwrote in message
news:uP**************@TK2MSFTNGP04.phx.gbl...
Hi,

If I don't call Quit, it will raise a problem:
In a scenario no Word is running, the user runs my application only and finishes my
application, and then there will be a WinWord.exe instance in the Task Manager after
quitting my application.

YOU need to keep track of this in your application. If you are binding to a Running Object
(the method I have shown) you should set a flag.
When terminating the application, you simply have to check the flag and call Quit when this
flag is not set.

Willy.
PS. Once again, please don't top post.

Mar 2 '07 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Microsoft | last post: by
3 posts views Thread by Adam Faulkner via DotNetMonster.com | last post: by
1 post views Thread by Adam Faulkner via DotNetMonster.com | last post: by
3 posts views Thread by Niyazi | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.