473,394 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 5253
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Microsoft | last post by:
I'm trying to display a word document inside a web page, but everytime I do I get this error: Error Type: Microsoft VBScript runtime (0x800A0046) Permission denied: 'CreateObject' Does...
4
by: Otis Hunter | last post by:
I have been given an Access Database which contains a table that has an OLE object field that contains a Word document. That table contains hundreds of records. I would like to find out how I can...
7
by: Dave | last post by:
Apologies for the newbie question. I have created a vb.net program for my company that is designed to work with Word Templates (about forty of them that we commonly use) that are selected by the...
3
by: Adam Faulkner via DotNetMonster.com | last post by:
I want to create a method within a class that opens a Microsoft Word 2000 Document and has the facility to Create a new word document and then extract a Page that exists within the original Word...
1
by: Adam Faulkner via DotNetMonster.com | last post by:
I had a problem before extracting pages from an existing word document and then inserting the content into a new word document. The following code below works with Microsoft Word 2000 Function...
3
by: Niyazi | last post by:
Hi, Its like a I am searching alot but still not found or satisfy what I found it. My question is that I had SQL server that contains some data. I also have a application folder call...
2
by: Colin Halliday | last post by:
I have a Word 2003 mail merge main document (form letter) that is linked to another Word document data source for the mail merge. If I open this doc using the Word GUI, it first asks me to...
0
by: Niyazi | last post by:
Hi, I created application that store the data in SQL SERVER that reside on network. The client also use this application to access the resources provided with application. But is the client want...
0
southoz
by: southoz | last post by:
Good ay all , I'm fairly new to access(a little over 5 weeks now). Since I'v started I have picked up a lot of useful information from forums such as this and in doing so will share that information...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.