Results 1 to 7 of 7
  1. #1
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: word quit event problem (Word 2000+)

    You more or less asked the same two-and-a-half months ago in <post:=511,066>post 511,066</post:>. I fear this is not easy.
    There's a fairly long thread from 2003 that may be interesting: <post:=243,841>post 243,841</post:>, and perhaps you can find something useful in the links in <post:=506,043>post 506,043</post:>.

  2. #2
    New Lounger
    Join Date
    Aug 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: word quit event problem (Word 2000+)

    Yeah, I recalled asking the question, but was hoping to revisit it.

    Currently my best avenue appears to be attempting to create a separate word addin that lives within the word process (my existing addin is an external EXE), and using it to pass information back to the existing add-in by hooking into the low level windows events (ie. via a wndproc). I'm not 100% sure this is going to work, but I'm certainly going to give it a try.

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: word quit event problem (Word 2000+)

    How are you loading the document in the first place? Can your intermediate COM object keep an object reference to the instance of Word that has the document and deal with restarting Word so that your application doesn't have to worry about it?

  4. #4
    New Lounger
    Join Date
    Aug 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: word quit event problem (Word 2000+)

    The problem with trying to mediate the re-creation of the word instance is that it simply shifts the race condition from the application to the COM component. Regardless of which side it is dealt with on, it is still possible to allow the application to attempt to open a new document up while Word is in the middle of a quit sequence.

    To clarify why, the application never has more than 1 document open via the COM component at a time. When that document is closed, the application is allowed to open another. This is all well and good, excepting that, as stated, the application finds out the document has closed before it finds out that the reason for this is that Word is quitting.

    So, even if I put in code to auto-reinit the word instance and such, it wouldn't solve the underlying problem. A user could still kill word, and thus the current open document would close, and then attempt to open another document, and the application would allow it, because it wouldn't yet know that Word was quitting.

  5. #5
    New Lounger
    Join Date
    Aug 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    word quit event problem (Word 2000+)

    <P ID="edit" class=small>(Edited by jscher2000 on 07-Nov-05 09:21. Added x-ref to earlier thread.)</P>I'm currently having a problem related to an external application making use of a COM component I've written that interfaces with word.

    Specifically, if a user closes word by clicking the X in the upper right of the window, I'm having difficulty making sure that the external application learns that Word is quitting due to user interaction prior to attempting to use my component again. This is a problem because if the user does close out Word, the external application needs to reinitialize my component before using it.

    Communication between the two (app and component) is done via messages. The app calls into the component to open up a document, and blocks until the user is done editing the document. When they exit Word, an event is sent back indicating what they chose to do (save, close without saving, etc).

    The problem is that when the user quits word using the X in the upper right, the order in which the component receives Word's messages makes it not obvious how to get the information to the app in a timely manner.

    What happens is this:

    1. Word fires it's DocumentBeforeClose event. If the document about to close is our document, we make a note of that.
    2. Word fires it's DocumentChange event. If the document mentioned is the one we have that is closing, we send an event to the app telling it the document has closed.
    3. At some indeterminate point in the future, Word may or may not fire it's Quit event. If it does, the app needs to not try to open any more documents without a reinit. If it doesn't, the app can open another document.

    And that's the crux of the problem. When we reach #2, we know our document is closing, but we don't know that Word is quitting. There is no good way that I can tell, at that point, to tell the application that Word will be quitting imminently. If Word is about to quit, but the application tries to open another document, it's going to catch us in a bad state, as Word will be in the process of killing itself while we are trying to open a document.

    The only way around this I can think of is a hardcoded delay in the application, which is rather messy and basically a race condition waiting to happen. Any ideas? Is there a way, at #2, that I can know that this isn't just a closing document, but that Word is in fact quitting?

    Related Earlier Thread: knowing if Word is quitting in DocumentChange (Wor

  6. #6
    New Lounger
    Join Date
    Aug 2005
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: word quit event problem (Word 2000+)

    Well, I've got a solution that does what I want, and I thought it might be interesting to others.

    What I ended up doing was creating a new word add-in (template) that has the following code:

    <pre>Option Explicit

    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
    (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

    Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" _
    (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal MSG As Long, _
    ByVal wParam As Long, lParam As Long) As Long

    Private Const GWL_WNDPROC = -4
    Private Const WM_DESTROY = &H2
    Private Const WM_CLOSE = &H10

    Private m_lpPrevWndProc As Long
    Private m_bQuitting As Boolean

    Public Sub InitializeQuitCatcher(ByVal hWnd As Long)
    m_lpPrevWndProc = SetWindowLong(hWnd, GWL_WNDPROC, _
    AddressOf QuitCatcherWndProc)
    m_bQuitting = False
    End Sub

    Public Function IsWordQuitting() As Boolean
    IsWordQuitting = m_bQuitting
    End Function

    Private Function QuitCatcherWndProc(ByVal hWnd As Long, ByVal uMsg As Long, _
    ByVal wParam As Long, lParam As Long) As Long

    QuitCatcherWndProc = 0

    Select Case uMsg
    Case WM_CLOSE
    m_bQuitting = True
    SetWindowLong hWnd, GWL_WNDPROC, m_lpPrevWndProc
    End Select

    QuitCatcherWndProc = CallWindowProc(m_lpPrevWndProc, _
    hWnd, uMsg, wParam, lParam)

    End Function</pre>


    I decided to go with a fairly simple method of finding out whether Word is quitting (ie. no passed in reference) because I may want to use it again elsewhere. The basic method of using it in your code is:

    Load the add-in when the document is loaded
    get the hWnd to the Word window (an exercise left to the reader)
    call 'InitializeQuitCatcher' with the hWnd
    Wherever you need to check whether word is quitting, call 'm_appWord.Run "IsWordQuitting"'

    Note that you need to re-init the quit catcher after restarting word, to give it the new hWnd.

    The only place I'm actually calling 'IsWordQuitting' is in DocumentBeforeClose, because it allows me to then signal the calling application that Word is quitting, and that it should wait until the quit sequence is done before attempting to open new documents. This is perfect for my needs, because the calling application does not attempt to open a new document until signaled that the current one is closed. So, now we can tell it about the impending quit before we tell it about the closing of the document. For other situations, you may need to check elsewhere, or you might want to modify this to have a reference to an object to signal so it can be handled asynchronously.

  7. #7
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: word quit event problem (Word 2000+)

    Thanks for sharing!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •