Why does the Word object model work this way? 
Author Message
 Why does the Word object model work this way?

When the following is executed:

Set m_WordApp = New Word.Application

We kind of hoped that you got a private instances of Word in our VB app. Not
so we discover - you appear to share it with the desktop copy of Word. This
leads to a few {*filter*} headaches:

1. Load our app
2. Do Set m_WordApp = New Word Application
3. Leave app running
4. Load Word externally
5. Close Word

Word kindly (!) closes the copy of Word running inside our app so the next
reference to m_WordApp falls over with a "server gone away" error. Even
trapping the Quit event doesn't help as you don't seem to be able to do a
Set m_WordApp = New Word.Application again. Even if you could, it would be
dangerous as Word might externally unload the Word central core half way
through our app doing something.

The workaround we've got is to open a read-only document at the same time
and leave it open whilst our app is running. This has the result of Word not
closing down when you exit Word externally (as there's a document open).

Why does Word work like this? Is it by design or is it a side effect in Word
2000 of the way they introduced the mechanism whereby documents open in
separate Windows but I'm guessing sharing the same Word central code.

Any way of forcing a private copy of Word?

Cheers, Rob.



Tue, 12 Oct 2004 19:50:32 GMT  
 Why does the Word object model work this way?
What version of VB are you running? With my VB6, I get a distinct copy of
Word: I have a form with three buttons, and this code:

Option Explicit

Private mWord As Word.Application

Private Sub Command1_Click()
Set mWord = New Word.Application
End Sub

Private Sub Command2_Click()
    mWord.Documents.Add
End Sub

Private Sub Command3_Click()
    MsgBox "Docs = " & mWord.Documents.Count
End Sub

The value returned by Command3 is independent of any documents opened and
closed using the main Word interface; and selecting Exit from Word's file
menu does not kill the application reference. Nor does it make any
difference whether Word or the app is started first.

But if you really want, you can shell Word with a command-line argument that
forces a new instance (can't remember what it is ... never needed it).


Quote:
> When the following is executed:

> Set m_WordApp = New Word.Application

> We kind of hoped that you got a private instances of Word in our VB app.
Not
> so we discover - you appear to share it with the desktop copy of Word.
This
> leads to a few {*filter*} headaches:

> 1. Load our app
> 2. Do Set m_WordApp = New Word Application
> 3. Leave app running
> 4. Load Word externally
> 5. Close Word

> Word kindly (!) closes the copy of Word running inside our app so the next
> reference to m_WordApp falls over with a "server gone away" error. Even
> trapping the Quit event doesn't help as you don't seem to be able to do a
> Set m_WordApp = New Word.Application again. Even if you could, it would be
> dangerous as Word might externally unload the Word central core half way
> through our app doing something.

> The workaround we've got is to open a read-only document at the same time
> and leave it open whilst our app is running. This has the result of Word
not
> closing down when you exit Word externally (as there's a document open).

> Why does Word work like this? Is it by design or is it a side effect in
Word
> 2000 of the way they introduced the mechanism whereby documents open in
> separate Windows but I'm guessing sharing the same Word central code.

> Any way of forcing a private copy of Word?

> Cheers, Rob.



Tue, 12 Oct 2004 20:43:28 GMT  
 Why does the Word object model work this way?

Quote:
> What version of VB are you running?

VB6.

Quote:
>I get a distinct copy of Word:

It may look like you have a distinct copy but you haven't. Open Word 2000
externally and you have only one copy of WINWORD.EXE process running.

Quote:
>I have a form with three buttons, and this code:

If I do this:

1. Click Command1
2. Click Command2
3. Open Word
4. Click Command3

I get a value of two, i.e. it's sharing it with the shell copy of Word.

But... in this standalone testbed, if you add a mWord_Quit event handler,
the quit event doesn't fire if you:

1. Click Command1
2. Open Word
3. Close Word

However, in our real application, the quit event does fire when external
Word is closed. So for some reason, it's working differently in a large
complex app compared to a small standalone. Don't you just hate that!

I thought it might be because we're actually doing set mWord = new
Word.Application inside a DLL called from the main app (we've encapsulated
Word in our own class) but that didn't seem to make much difference. I am
actually testing this on my home PC - will try and same example at work
after the weekend.

Later..

Rob.



Wed, 13 Oct 2004 19:01:40 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. 3 ways to create a Word object?

2. Report Object Model/Viewer Object Model

3. Why does this piece of code work with Word 97 but not Word 2000

4. Valid script does not work with Scripting Object Model

5. Word object model hWnd

6. Distribution Issues Of A Sotware Using Microsoft Word Object Model

7. Interface Confusion in the Word Object Model

8. Word object model

9. Doc->XML, Word Object-Model Problems

10. New book release includes graphical Word object model

11. Word Object Model

12. Word Object Model to Compose Letter

 

 
Powered by phpBB® Forum Software