You Cannot Close Microsoft Word Because A Dialog Is Active

B

Bruce Maston

I get the following message when I use automation to open an existing Word
Document from Access (and then try to close the document after making
changes):

Set objWord = CreateObject("Word.Application")
objWord.Documents.Open (ExistingDocument)
objWord.ActiveDocument.Activate
objWord.ActiveWindow.WindowState = wdWindowStateMaximize
objWord.Visible = True

When I close the word domument, the message pops up. When you click "ok,"
you get to the next box that asks if you want to save changes.

It's more a cosmetic nuisance than anything, but there must be something
missing in my automation that is doing this.

Thank you for any insights.
 
P

Perry

Try to save the Word document in code before closing, as in:
objWord.ActiveDocument.SaveAs "c:\MyPath\MyDocName.doc"
objWord.ActiveDocument.Close

If not needed to save the document, close the document without saving the
changes, as in:
objWord.ActiveDocument.Close 0


--
Krgrds,
Perry

System:
Vista/Office Ultimate
VS2005/VSTO2005 SE
 
B

Bruce Maston

Hi Perry, Thank you for your reply.

This problem is allied with a couple of other situations I've encountered
with Access to Word automation. The others I've "solved" (for the most part)

In one situation, Automation opens a Word template with
Set objWdDoc = objWord.Documents.Add (Template:= "Template.dot").
When you try to Quit/Close Word you get a message that Normal.dot has been
altered and do you want to save it. You have to click "No" and "cancel" on a
number of screens to get rid of this. This is solved by including in the
close/quit events of the word template
NormalTemplate.Saved = True.
I understand this works because it automatically answers the question so the
box never appears.

In the second situation, you are trying to capture information from the Word
Document at the time it is saved and closed. The "best" way is to use
WithEvents in Access so that Access "knows" when the document is being
closed. Another way is to include a While/Wend loop in Access coupled with
an on error event. Access "hangs" at the While/Wend as long as the Word
Document is open, but when the document is closed it creates an error that
gets you out of the loop. Thus:
While objWord.IsObjectValid(objWdDoc)
'MS Help [reference library]
'when Word is closed, there is an error
'to get you out of this loop
Wend

(I've included what I've learned over time with this for anybody with a
similar problem who stumbles on this thread.)

I'll take some time today to study how I might use your suggestion in
conjunction with one of the other "solutions." The problem is that Access
"runs ahead" of the automation. Access will execute the rest of the lines in
the procedure before the Word Document or Template is even open. You have to
figure out a way to stop Access, and the only ways I've learned so far are
the While/Wend and WithEvents. And, frankly, these solutions are
unsatisfactory because WithEvents is heavy duty automation that is tricky to
program and While/Wend locks Access as long as the Word document is open.

The other problem is that I don't know how you would put a command like the
NormalTemplate.Saved = True in the finished document (rather than in the
template that created the document.)

Fundamentally, I don't understand how Word retains a "memory" that it was
opened via Automation (rather than simply being opened with double-click on
the file). How come Word "knows" that there is some kind of "active dialog,"
and shouldn't there be a line of code that you insert in Access that simply
turns off the automation once the document is opened. Something like
objWord.SeeYaLater = True.
 
P

Perry

Usually, if coded correctly, and you're using below code lines to
manipulate a Word document based on a different (!) template than normal.dot
In one situation, Automation opens a Word template with
Set objWdDoc = objWord.Documents.Add (Template:= "Template.dot").

and you're not changing any other document in same automation code sequence
besides objWdDoc
this line will absolutely be redundant
NormalTemplate.Saved = True.

Yet, if you're automation code still requires you to validate the changes
made to normal.dot,
the automation code you're using -most likely- is not correct.

--
Krgrds,
Perry

System:
Vista/Office Ultimate
VS2005/VSTO2005 SE



Bruce Maston said:
Hi Perry, Thank you for your reply.

This problem is allied with a couple of other situations I've encountered
with Access to Word automation. The others I've "solved" (for the most
part)

In one situation, Automation opens a Word template with
Set objWdDoc = objWord.Documents.Add (Template:= "Template.dot").
When you try to Quit/Close Word you get a message that Normal.dot has
been
altered and do you want to save it. You have to click "No" and "cancel"
on a
number of screens to get rid of this. This is solved by including in the
close/quit events of the word template
NormalTemplate.Saved = True.
I understand this works because it automatically answers the question so
the
box never appears.

In the second situation, you are trying to capture information from the
Word
Document at the time it is saved and closed. The "best" way is to use
WithEvents in Access so that Access "knows" when the document is being
closed. Another way is to include a While/Wend loop in Access coupled
with
an on error event. Access "hangs" at the While/Wend as long as the Word
Document is open, but when the document is closed it creates an error that
gets you out of the loop. Thus:
While objWord.IsObjectValid(objWdDoc)
'MS Help [reference library]
'when Word is closed, there is an error
'to get you out of this loop
Wend

(I've included what I've learned over time with this for anybody with a
similar problem who stumbles on this thread.)

I'll take some time today to study how I might use your suggestion in
conjunction with one of the other "solutions." The problem is that Access
"runs ahead" of the automation. Access will execute the rest of the lines
in
the procedure before the Word Document or Template is even open. You have
to
figure out a way to stop Access, and the only ways I've learned so far are
the While/Wend and WithEvents. And, frankly, these solutions are
unsatisfactory because WithEvents is heavy duty automation that is tricky
to
program and While/Wend locks Access as long as the Word document is open.

The other problem is that I don't know how you would put a command like
the
NormalTemplate.Saved = True in the finished document (rather than in the
template that created the document.)

Fundamentally, I don't understand how Word retains a "memory" that it was
opened via Automation (rather than simply being opened with double-click
on
the file). How come Word "knows" that there is some kind of "active
dialog,"
and shouldn't there be a line of code that you insert in Access that
simply
turns off the automation once the document is opened. Something like
objWord.SeeYaLater = True.

Bruce Maston said:
I get the following message when I use automation to open an existing
Word
Document from Access (and then try to close the document after making
changes):

Set objWord = CreateObject("Word.Application")
objWord.Documents.Open (ExistingDocument)
objWord.ActiveDocument.Activate
objWord.ActiveWindow.WindowState = wdWindowStateMaximize
objWord.Visible = True

When I close the word domument, the message pops up. When you click
"ok,"
you get to the next box that asks if you want to save changes.

It's more a cosmetic nuisance than anything, but there must be something
missing in my automation that is doing this.

Thank you for any insights.
 

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