• CloseFile, Document_Close, & closing files in code

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » CloseFile, Document_Close, & closing files in code

    Author
    Topic
    #383653

    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!

    Viewing 4 reply threads
    Author
    Replies
    • #654965

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

      Gary

      • #655489

        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…)

    • #654977

      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.

    • #655031

      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? 2cents

      greetings, pieter.

    • #655105

      >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.

    • #655439

      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.

    Viewing 4 reply threads
    Reply To: CloseFile, Document_Close, & closing files in code

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: