Problem programmatically opening AutoOpen enabled document

K

kai.iske

Hi,

I have two documents. One that contains an AutoOpen implementation much
like this:

Sub AutoOpen()

...
' Do some magic here which is of no relevance to the problem
...

ThisDocument.Close wdDoNotSaveChanges

End Sub

The other document / add-in contains a method that loads in the first
document and then attempts to continue to do its processing. Here's a
sample "implementation" of that method

Sub Test()

Application.Documents.Open ("AutoCloseTest.doc")
MsgBox "Hello"

End Sub
(Assumption is that the document containing the call to
ThisDocument.Close() in its AutoOpen metho is called AutoCloseTest.doc
and located in the same directory)

I stripped it down to hopefully make it clear. When I debug the Test
method in VBA and execute the Open(...) line I do see that the AutoOpen
enabled document gets opened and will close itself after processing.
However the VBA debugger does not continue to execute the MsgBox
"Hello" line.

Anyone out there who knows the reason for this behavior? My real code
is a bit more complex and involves a global Add-In that in turn
registers a menu and initializes a C++ based COM Server CoClass. When
this Add-In attempts to open the "AutoCloseTest.doc", I see my COM
Server CoClass being released as if the Add-In gets reinitialized. I
guess this all has something to do with the strange behavior outlines
above.

Any help would be highly appreciated.

Thanks in advance

Kai Iske
 
C

Cindy Meister

Both oddities you describe (the VBA code not continuing, the COM Add-in
"dropping out") make me suspect that the AutoOpen code is failing silently.
I'm guessing the problem is trying to close the document before it's been
fully opened and initialized.

If you alter the Test procedure

dim doc as Word.Document
Set doc = Documents.Open("AutoCloseTest.doc")
doc.Close wdDoNotSaveChanges
MsgBox "Hello"

and then remove the Close command from the AutoOpen procedure, do things
behave the way you'd expect?
 
J

Jezebel

I can't find the reference, but I'm sure I've seen an MSDN article about why
you shouldn't close a document in its own AutoOpen procedure ... which
instinctively seems worng, to say the least.
 
K

kai.iske

Cindy, I stripped down the AutoOpen code to just call
ThisDocument.Close() so I guess there can't be anything wrong with it
:) Yes, the code continues to work / process if I remove the call to
Close() from the AutoOpen.

Jezebel, Cindy, I guess both of you are right as it isn't a nice idea
to close a document while in the process of being opened. My assumption
is that the Document CoClass instance will delete itself because of the
call to Close() and VBA attemtps to continue working with the Interface
reference it acquired during the Open() call and this makes the VBA/VBE
engine die silently.

However, anyone can think of an approach to somehow implement the
desired functionality of having a document that, when opened, will
execute some magic and will then be forced to close automatically?

Also, if anyone has a pointer to the MSDN article Jezebel mentioned (I
didn't find it), I could go back to my customer and at least tell them
and probably suggestion a workaround.

Thanks in advance

Kai
 
J

Jean-Guy Marcil

(e-mail address removed) was telling us:
(e-mail address removed) nous racontait que :
Cindy, I stripped down the AutoOpen code to just call
ThisDocument.Close() so I guess there can't be anything wrong with it
:) Yes, the code continues to work / process if I remove the call to
Close() from the AutoOpen.

Jezebel, Cindy, I guess both of you are right as it isn't a nice idea
to close a document while in the process of being opened. My
assumption is that the Document CoClass instance will delete itself
because of the call to Close() and VBA attemtps to continue working
with the Interface reference it acquired during the Open() call and
this makes the VBA/VBE engine die silently.

However, anyone can think of an approach to somehow implement the
desired functionality of having a document that, when opened, will
execute some magic and will then be forced to close automatically?

Also, if anyone has a pointer to the MSDN article Jezebel mentioned (I
didn't find it), I could go back to my customer and at least tell them
and probably suggestion a workaround.

The only way this makes sense to me is when you call such a "magic" document
from another routine.

A user will not want to open a document just to have it close itself as soon
as it opens. So, I do not think that it is necessary to have a close
statement in the Auto-Open sub.

In fact, all the code you want to execute when opening the document could be
in the same project that contains the calling sub. No need to have any code
in the Auto-open, unless that code is needed when the same document is
called from other routines in other projects.


--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
K

kai.iske

Jean-Guy,

agreed, so I will have to talk my customer through this and we would
then have to find a workaround to the problem.

However the pointer to that MSDN article would be great :)

Regards

Kai
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top