Consulting

Results 1 to 3 of 3

Thread: Is there any event allowing Cancel of closing a Window (not closing a Workbook)?

  1. #1
    VBAX Regular
    Joined
    Aug 2019
    Posts
    9
    Location

    Is there any event allowing Cancel of closing a Window (not closing a Workbook)?

    (FYI this is different than my recent post) If I open two windows on one workbook, such as
    Control-N
    alt-W-N
    then you can close a Window with Control-F4 or Control-W. The workbook is still open but the window closes. So you'd be closing Book1:2 but book1.xls is still open.
    I want to Cancel that closing if certain conditions are true. Can I?

    The only event that I know of when you close a window is App_WindowDeactivate. However, it doesn't seem to be Cancel-able. Also a problem is that that event happens every time you flip to another window or workbook. I'm only interested in acting on the case where a Window is being closed, such as by Control-F4 or Control-W, but I don't see how to identify that the user has gone (e.g.) Control-F4, as opposed to Control-Tab or something (that's not Closing the window).

    All this is for MDI versions of Excel, prior to when they destroyed a lot of VBA code with SDI -- 2013 IIRC.

  2. #2
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello LarryG,

    There are no native VBA events for closing a Window. However, you could intercept the close message by sub-classing the Window Procedure for the window you don't want to close. This requires writing VBA macros that call the Windows API. If you are not familiar with the Windows API and Window objects then you will need to work with someone who does.
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  3. #3
    VBAX Regular
    Joined
    Aug 2019
    Posts
    9
    Location
    Leith - thanks for the comment. I've implemented a few things like SetEnvironmentVariable, GetForegroundWindow, GetAsyncKeyState, SetWindowPos. I struggle with Callbacks, and hooks scare me, but other than that, doing them in VBA _seems_ easy. And VBA seems quite forgiving; long ago I would write API calls in C and data types (short/int/long/HWND/whatever) could really kill you but to my recollection VBA doesn't seem to. Maybe it's just because I've been 32 for ages (though I also use 64 Win 10 and XL16).

Posting Permissions

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