Results 1 to 8 of 8
  1. #1
    4 Star Lounger
    Join Date
    Jan 2002
    Location
    London, Gtr London, England
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    DocumentBeforeClose invoked several times (2000/2003/...)

    Very wide screenshot cropped by HansV - Thanks & sorry. Edited: Problem 4 added.

    Hi

    I have an interesting problem - hope you guys can shed some light on it. I have defined 3 sub in my template. I want to make sure that the TOC is updated before the document is closed (not when document saved by the user pressing Ctr-S or File|Save or File|SaveAs), i.e. only generate TOC when the document is CLOSED.

    The problem:
    1. I have several documents opened (say 3 for the sake of argument)
    2. I edit document 1
    3. I press Alt-F4 (to close the edited document)
    4. Word seems to invoke appWrd_DocumentBeforeClose three times!!! Every time it goes into appWrd_DocumentBeforeSave I print out the name of the document and the .saved flag value. Everytime the document is the same. Why is it invoking it 3 times? Should it not be just once?

    Private Sub Document_New
    If appWrd Is Nothing Then
    MsgBox ("Document_New + appWrd set up")
    Set appWrd = Word.Application
    SetWord_AND_DocumentOptions ' Set up word & document options
    AddDocVariables ' Add active doc variables for use
    InitAndDisplayDocPropeties (fNewDocument) ' Initialise & display doc properties
    RestoreMainView ' Set view to print layout
    End Sub

    Private Sub Document_Open
    If appWrd Is Nothing Then
    MsgBox ("Document_Opened + appWrd set up")
    Set appWrd = Word.Application
    SetWord_AND_DocumentOptions ' Set up word & document options
    AddDocVariables ' Add active doc variables for use
    InitAndDisplayDocPropeties (fOpenDocument) ' Initialise variable & display doc properties
    End If
    End Sub

    Private Sub appWrd_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
    MsgBox ("appWrd_DocumentBeforeClose " & Doc.Saved & ActiveDocument.Saved)

    If Doc.Saved = False Then
    MsgBox ("Document edited - needs to be saved - Generate TOC")
    Generate_TOC
    UpdateAllFields
    End If
    End Sub

    Problem 2: The menu displays 'Window' option - I didn't get it before
    Problem 3: I have Adobe PDF maker installed as part of the WORD setup - its menu options are added twice
    Problem 4: I open another document using totally different template. When I try to close that one - it still goes into appWrd_DocumentBeforeClose? Doesn't make sense as it does go into Document_Open or Document_New???? Very confusing.

    Thanks

    Robie
    Attached Images Attached Images
    Thanks.
    Robie

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

    Re: DocumentBeforeClose invoked several times (2000/2003/...)

    Problem 2. Window is a standard menu in Word. It contains options to rearrange windows and lists all open windows.

    Problem 3: See Phil Rabichow's Systematic Approach to Behavioral Problems in Word.

  3. #3
    4 Star Lounger
    Join Date
    Jan 2002
    Location
    London, Gtr London, England
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: DocumentBeforeClose invoked several times (2000/2003/...)

    Thanks Hans. Problem 2: I thought I didn't see it before in Word 2003. I must have been wrong. BTW: I have added problem 4 to my original post.
    Thanks.
    Robie

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

    Re: DocumentBeforeClose invoked several times (2000/2003/...)

    Problem 1: When I try this, the appWrd_DocumentBeforeClose event runs only once. If it runs three times when you try it, there is probably something in the macros you call from appWrd_DocumentBeforeClose that causes this.

    Problem 4: an application-level event handler will run for all documents, not just documents based on your template. If you want to run code only if the document is based on your template, use the Document_Close event procedure in ThisDocument (similar to Document_New and Document_Open).

  5. #5
    4 Star Lounger
    Join Date
    Jan 2002
    Location
    London, Gtr London, England
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: DocumentBeforeClose invoked several times (2000/2003/...)

    Hans you said: Problem 1: When I try this, the appWrd_DocumentBeforeClose event runs only once. If it runs three times when you try it, there is probably something in the macros you call from appWrd_DocumentBeforeClose that causes this.

    ----------
    I thought of that as well. So edited the template and removed everything from the appWrd_DocumentBeforeClose except a msgbox. Created two brand new documents. It still invokes it based on number of documents opened. So if 4 documents are opened - it invokes it 4 times.

    Private Sub appWrd_DocumentBeforeClose(ByVal Doc As Document, Cancel As Boolean)
    MsgBox ("appWrd_DocumentBeforeClose " & Doc.Saved & ActiveDocument.Saved)
    '
    ' If Doc.Saved = False Then
    ' MsgBox ("Document edited - needs to be saved - Generate TOC")
    ' Generate_TOC
    ' UpdateAllFields
    ' End If
    End Sub

    Attached doc shows what the two macros do - quite simple really but...

    Thanks

    Robie
    Attached Files Attached Files
    Thanks.
    Robie

  6. #6
    4 Star Lounger
    Join Date
    Jan 2002
    Location
    London, Gtr London, England
    Posts
    416
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: DocumentBeforeClose invoked several times (2000/2003/...)

    Hans

    I restarted with a brand new template & stripped out everything except Document_Open, Document_New & appWrd_DocumentBeforeClose. I have attached it. Using the attached template open two documents and press Alt-F4 - appWrd_DocumentBeforeClose invoked twice. If I had three documents open it would be invoked 3 times & so on.

    Totally baffled now. <img src=/S/hairout.gif border=0 alt=hairout width=31 height=23>

    Robie
    Attached Files Attached Files
    Thanks.
    Robie

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

    Re: DocumentBeforeClose invoked several times (2000/2003/...)

    You have set it up in such a way that a new instance of appWrd is created for each document. You should use a single instance in a class module instead. See the attached zipped template for the correct setup with a class module.
    Attached Files Attached Files

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

    Re: DocumentBeforeClose invoked several times (2000/2003/...)

    See my previous reply. But why not use the Document_Close event instead of appWrd_DocumentBeforeClose? Do you really want to run the code for *all* documents?

Posting Permissions

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