Results 1 to 7 of 7
  1. #1
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post

    CloseFile, Document_Close, & closing files in code

    I've encountered a group of weird problems while trying to do something that seems simple: closing a document in a macro.

    Here's the situation. My application may be used in several Word documents at once, any of which may use a special temporary document as a scratch area. The temporary document is created automatically when the application's first "real" document is opened. It should be closed automatically when the application's last document is closed.

    The first problem I discovered is that Word calls its event macros inconsistently. When I close a document from the menu, it calls FileClose. When I close a document by clicking the close box, it does not call FileClose, but it calls Close_Document. And when I close the last document opened, it calls both.

    This creates a nightmare for my program logic. To ensure that my "on close" logic will be executed no matter how a document is closed, I must call it from both FileClose and Close_Document. But then, when I close the last document, it is executed twice. I haven't figured out a clean, reliable way to detect when this is happening and avoid doing things twice that should only be done once.

    The second problem is that the sequence of operations breaks Word's Multiple Document Interface logic. This happens when only two documents remain open, one being an application document and the other the temporary document. Apparently what happens is this: when I close the last application document, Word says to itself, "OK, this is not the last document open, so I should close the window when I close the document." Then my close logic closes the temporary document, so that by the time Word gets around to closing the application document, it IS the last document. Word closes the window along with the document, leaving no document windows open, but doesn't shut itself down! (If the VBA editor window is open, stays open.) Word then crashes.

    I'm looking for some way to be sure that the temporary document is closed along with the last application document, and also to keep Word from climbing into a hole and then covering itself up!

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: CloseFile, Document_Close, & closing files in code

    Would any of the application-level events be any use? - for instance DocumentBeforeClose (assuming you have Word 2000 or later).

    Gary

  3. #3
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: CloseFile, Document_Close, & closing files in

    I wasn't aware of that group of events, but they look like they have possibilities. I'll try DocumentBeforeClose and report what I find.

  4. #4
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Brussels, Brussel, Belgium
    Posts
    159
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: CloseFile, Document_Close, & closing files in code

    hi,

    a quick test shows me document_close is also triggered when you close a document from the menu (if it has been saved or not doesn't affect this behavior). this would mean document_close is triggered in all cases. couldn't you just put your code under this event only then, and leave the fileclose for what it is? <img src=/S/2cents.gif border=0 alt=2cents width=15 height=15>

    greetings, pieter.

  5. #5
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: CloseFile, Document_Close, & closing files in

    >a quick test shows me document_close is also triggered when you close a document from the menu...
    >couldn't you just put your code under this event only then, and leave the fileclose for what it is?

    Pieter, that is certainly how it OUGHT to work. I believe you when you say that it works that way on your system, but it does not work that way on mine. This is not a vague impression; I tested and confirmed it.

    I wish I knew why, and even more, how to fix it. I'm running Word 2000 SR1 under Windows 2000 with Service Pack 2. Any ideas?

    Meanwhile, I tried the DocumentBeforeClose event and found that it is called exactly once at all the right times, with no untoward side effects. I haven't tried to adapt my logic to it yet, but I'm very optimistic (I hate to say "confident") that it will solve the problem.

  6. #6
    4 Star Lounger
    Join Date
    Mar 2002
    Location
    Sacramento, California, USA
    Posts
    509
    Thanks
    4
    Thanked 1 Time in 1 Post

    Re: CloseFile, Document_Close, & closing files in

    The code that uses DocumentBeforeClose is tested and in service now, and it appears to be working beautifully!

    I realized that another event in this group, WindowActivate, solves a long-standing problem: how to update a command bar in several similar documents when one of them is changed. I knew this was the clean way to do it, but until now I didn't know how to make it work.

    Thanks for pointing this out. It will make some of my coding a lot easier.

  7. #7
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: CloseFile, Document_Close, & closing files in code

    You are absolutely right on with this one too, Gary. Just last week I was going to
    post my *big discovery* -- that the only way to handle ALL the close events (with
    dirty or none-dirty documents) is to use DocumentBeforeClose. I suffered for years
    in Word 97 (where there's no such animal), but had to find an answer during
    the .Net cutover. I was coding up all those darned clicked events for File|Close,
    the toolbar buttons, etc., etc. but still could do nothing if the user clicked the Big X
    on a dirty document -- bypasses my close code and therefore my save code as well.

    All that to say, the answer is DocumentBeforeClose for all your custom save or close
    events needs.

    Hey, why didn't you tell me this before? (You probably did, but I had Office 97 ear muffs on...)
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

Posting Permissions

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