PDA

View Full Version : Solved: On document_close() Quit



Levski
04-29-2005, 01:32 PM
I would like the document_close() sub to make the Word Application quit without affecting any other instances of Word that may be running.

Can this be done? My thanks in advance.

Levski.

Jacob Hilderbrand
04-29-2005, 03:41 PM
Try this:

Option Explicit

Private Sub Document_Close()

Application.Quit

End Sub

Levski
04-30-2005, 01:45 AM
Thanks for the response.

Unfortunately that method will close all instances of Word. I only wish to close the window containing the document with the macro.

As a sidenote, using application.quit also causes save commands to hang for several seconds.

Howard Kaikow
04-30-2005, 02:35 AM
Thanks for the response.

Unfortunately that method will close all instances of Word. I only wish to close the window containing the document with the macro.

As a sidenote, using application.quit also causes save commands to hang for several seconds.

u have to specify the correct application object.
if only 1 instance of word is running, than application.quit will close that instance. if there's more than 1 instance, u need to use

someappobject.quit

whe someappobject is the instance of word in which the document was opened,

however, if u want to just close the document that should happen automatically unlss u do somethin in document_close or autoclose to prevent the close.

TonyJollans
04-30-2005, 04:13 AM
Hi Levski,
Application.Quit
Will close the instance of Word in which it is run. Exactly that, no more, no less. I?m afraid I don?t understand what Howard is saying (?Application? is ?someappobject?).

If you are running Word 2000, or if you are running a later version and have ?Windows in Taskbar? set (under Tools > Options > View), then each open document will have its own taskbar icon and, if you have multiple documents open, you will be unable to tell directly from the UI how many instances of Word you are running. So, I think you are confusing Documents with instances of Word.

What exactly are the symptoms you are trying to address?

As regards your note that using application.quit causes the save to hang; well, that?s quite possible if it?s closing several other documents in order to quit the application at the same time.

Levski
04-30-2005, 05:45 AM
As Howard kindly pointed out, Doc.Close will automatically close the window when more than one Word window exists, so the solution I have settled on (pending any quirks that may arise) is if wordapp.documents > 1 then close else application quit.

As for the purpose, I am working on a contact manager in pascal that uses a Word link for its wp. I am latching onto Words built in automation server and passing in variables from my application. However, when the user has finished their letter and wants to close, I dont want the onclose > Quit event to inadvertantly close any other word documents they may have been working on.

Thanks for the pointers. Much appreciated.

Howard Kaikow
04-30-2005, 08:54 AM
As Howard kindly pointed out, Doc.Close will automatically close the window when more than one Word window exists, so the solution I have settled on (pending any quirks that may arise) is if wordapp.documents > 1 then close else application quit.

As for the purpose, I am working on a contact manager in pascal that uses a Word link for its wp. I am latching onto Words built in automation server and passing in variables from my application. However, when the user has finished their letter and wants to close, I dont want the onclose > Quit event to inadvertantly close any other word documents they may have been working on.

Thanks for the pointers. Much appreciated.

Proper programming dictates use the Close method on each document.
If you want to close Word and not be bothered by messages asking if you wannta save things, you can use the SaveChanges parameter for the Quit method.

appWord.Quit only quits the session specified by the appWord object.

TonyJollans
04-30-2005, 11:19 AM
Proper programming dictates use the Close method on each document.

Absolutely. The correct thing to code is
objDocument.Close
If appWord.Documents.Count = 0 Then appWord.Quit
Set objDocument = Nothing
Set appWord = Nothing
or whatever the Pascal equivalent is.

Levski
04-30-2005, 01:18 PM
That is pretty much what I settled on. Having it confirmed by a couple of word Gurus is very reassuring.

Thanks again. :thumb

Howard Kaikow
04-30-2005, 01:21 PM
Absolutely. The correct thing to code is
objDocument.Close
If appWord.Documents.Count = 0 Then appWord.Quit
Set objDocument = Nothing
Set appWord = Nothing
or whatever the Pascal equivalent is.

THat code is incorrect.
It's an error to set appword = nothing before quitting.

The bottom line is that an app has to keep track of what documents it has opened/created and take to to either make surethat they are all closed/saved prior to quitting, or just tell word to quit using the savechanges parameter. How this is done will vary from prog to prog,.

Levski
04-30-2005, 01:30 PM
I see your point, but the code is not being run within Word so worddoc & wordapp objects can be freed by the compiler after quitting Word (with the appropriate save/saveas parameters) so it is all working fine.

Thanks.

Howard Kaikow
04-30-2005, 01:54 PM
I see your point, but the code is not being run within Word so worddoc & wordapp objects can be freed by the compiler after quitting Word (with the appropriate save/saveas parameters) so it is all working fine.

Thanks.

does not matter whether the code is being run in word, you are still using a word application object.

TonyJollans
05-01-2005, 07:05 AM
Absolutely. The correct thing to code is
objDocument.Close
If appWord.Documents.Count = 0 Then appWord.Quit
Set objDocument = Nothing
Set appWord = Nothing
or whatever the Pascal equivalent is.
THat code is incorrect.
It's an error to set appword = nothing before quitting.

The bottom line is that an app has to keep track of what documents it has opened/created and take to to either make surethat they are all closed/saved prior to quitting, or just tell word to quit using the savechanges parameter. How this is done will vary from prog to prog,.I do not think my code is incorrect. My code does not set appword = nothing before quitting if for no other reason than that wouldn't work!

There are two situations. Either one wants to quit the Word application or one doesn't.

If one wants to quit it (in this case because there are no open documents) then my code quits it.

If one doesn't want to quit (because there are other documents still open) are you suggesting that the object pointer should not be set to Nothing? I must disagree. If it is not set to nothing there is the possibility that the (application) object will not be properly cleared from memory when the user (or other code) finishes with it and quits it.

Howard Kaikow
05-01-2005, 07:31 AM
If one doesn't want to quit (because there are other documents still open) are you suggesting that the object pointer should not be set to Nothing? I must disagree. If it is not set to nothing there is the possibility that the (application) object will not be properly cleared from memory when the user (or other code) finishes with it and quits it.

I forgot to respond to the above.

Setting appword = nothing does not cause that instance of word to be quit when the code terminates. You'd be left with a running winword.exe process for each instance of the app object not quit.

TonyJollans
05-01-2005, 07:36 AM
Setting appword = noting does not cause that instance of word to be quit when the code terminates. You'd be left wita running winword.exe process for each instance of the app object not quit.Absolutely correct and that is what the code intends to do. If there are other documents open within the instance of Word it does not want to be quit. The assumption when there are other documents open is that the code got a pointer to an existing instance of Word and, rather than quitting the app, it now wants to disassociate itself from it and leave it as it found it.

Howard Kaikow
05-01-2005, 07:55 AM
Absolutely correct and that is what the code intends to do. If there are other documents open within the instance of Word it does not want to be quit. The assumption when there are other documents open is that the code got a pointer to an existing instance of Word and, rather than quitting the app, it now wants to disassociate itself from it and leave it as it found it.

Each instance of word has its own documents collection.
The key is whether the instance defined by appword was created by the code, or inherited from elsewhere.

The document count does not provide that info.

A common use would be:

Public Sub MyCode (appWord as Word.Application, ...)
' Stuff
end sub

Or, even better, passing a Word application instance to a class.

MyCode has no business ever shutting down appWord even if the doc count gets to 0, that's a decision for the application that called MyCode.

TonyJollans
05-01-2005, 08:49 AM
There isn't a right and a wrong when you're dealing with people. There might be if code is the only concern but if you have an interactive environment users can open or close (other) documents while the code is running. Normal users do not know the difference between multiple documents and multiple instances of Word and expect the environment, as they see it, to behave as it would if the code weren't running. This means documents they open staying open and, normally, open instances of Word without open documents not hanging around. Checking for a document count of 0 may not be perfect but it's about the best you can do in some circumstances (unless you want to prompt the user with something like "it appears that ...")

If a routine is passed a pointer to an application, then that routine should not shut it down. Agreed. The decision must be made by the routine which establishes the connection to the application (whether by linking to an existing instance or creating a new one) and that decision may depend on environmental factors beyond its control. In some circumstances it is correct to terminate an app it didn't instantiate and in others it may be right to leave running an app it did instantiate.

Howard Kaikow
05-01-2005, 10:35 AM
There isn't a right and a wrong when you're dealing with people. There might be if code is the only concern but if you have an interactive environment users can open or close (other) documents while the code is running. Normal users do not know the difference between multiple documents and multiple instances of Word and expect the environment, as they see it, to behave as it would if the code weren't running. This means documents they open staying open and, normally, open instances of Word without open documents not hanging around. Checking for a document count of 0 may not be perfect but it's about the best you can do in some circumstances (unless you want to prompt the user with something like "it appears that ...")

If a routine is passed a pointer to an application, then that routine should not shut it down. Agreed. The decision must be made by the routine which establishes the connection to the application (whether by linking to an existing instance or creating a new one) and that decision may depend on environmental factors beyond its control. In some circumstances it is correct to terminate an app it didn't instantiate and in others it may be right to leave running an app it did instantiate.

Documents users open outside of the running app are not part of the documents collection within the instance of Word used by te running app, unless that instance is the same as the original Word app instance.

It is up to the code to handle the documents under its app instance.
It is neve racceptable for code to shut down an instance of am app it did not start without having some prior knowledge, which is ordinariy not available.

Try the following in a VB 6 Form.


Option Explicit
Private appWord As Word.Application

Private Sub btnByeBye_Click()
Unload Me
End Sub

Private Sub btnRunMe_Click()
appWord.Documents.Add
ActiveDocument.SaveAs FileName:="D:\Boo.doc"
appWord.Documents.Add

' The following is an error because apWord has not yet Quit
' A running Winword.exe process is left behind, along with
' work files in the Temp directory and, if any, Word's Startup directory,
' and for the open Boo.doc file
Set appWord = Nothing
End Sub

Private Sub Form_Load()
Set appWord = New Word.Application
End Sub

TonyJollans
05-01-2005, 11:23 AM
Documents users open outside of the running app are not part of the documents collection within the instance of Word used by te running app, unless that instance is the same as the original Word app instance.I'm sorry, Howard, but you're plain wrong on that. They may or may not be, depending on several factors.

Of course, your code as posted is the wrong thing to do - nobody's disagreeing with you on that. What I am saying is that there may be circumstances where one should leave an app running even if the code has invoked it. I think we will just have to agree to differ.

Howard Kaikow
05-02-2005, 09:02 AM
I'm sorry, Howard, but you're plain wrong on that. They may or may not be, depending on several factors.

Of course, your code as posted is the wrong thing to do - nobody's disagreeing with you on that. What I am saying is that there may be circumstances where one should leave an app running even if the code has invoked it. I think we will just have to agree to differ.

Well, I worship my religion and you worship yours.
I do so on the 8th day each week.