Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)... Cancel Argument ?

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,619
Office Version
  1. 2016
Platform
  1. Windows
Hi,

Contrary to many other excel object model events, the Window Deactivate event does not provide a ByRef Cancel argument.

I was wondering if there is some workaround so that we could choose whether to allow the workbook window to be closed or to abort the closing action.

I am targeting the Window Deactivate event because it also fires when attempting to close a workbook window.

Obviously, we are talking here about a workbook that has more than one window. Otherwise, we could use the BeforeClose event.

I am open to your suggestions.
Regards.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Any thoughts or innovative idea on this ?

As a last resort, I am thinking of setting up a hook in the Deactivate event to trap the closing action, but I am not sure that would work and even if that does work, I was hoping to find a lighter workaround.
 
Upvote 0
Hi Jaafar. I did give it some thought (and my thoughts/ideas could hardly be described as 'innovative'), but no luck I'm afraid. Sorry I couldn't be of any help.
 
Upvote 0
How would you determine that it was being closed, rather than just deactivated? Or does that not matter for your purposes?
 
Upvote 0
@Dan_W
Thanks for taking a look.

@RoryA
No. I does matter. I want to prevent only the closing action but not the "deactivation" of the window.

I have in mind defining a Boolean Property (something like CancelClosing=TRUE) that can be set inside the window deactivate event only when the user decides to close the window... This would require code to be able to distinguish between closing and deactivating the window.

This in theory, could be achieved by setting a windows hook or by subclassing the window and watching for the closing event but, the idea of having a hook set througout the live of the window can be problematic (has a performance hit and poses a risk of crashing the application) hence the idea to set the hook only inside the Deactivate event, that way the hook would be set only temporarly & would last a brieve moment only at the time the window is being deactivated.

I havent had the time yet to start implementing this idea but I doubt it will work.

Regards.
 
Last edited:
Upvote 0
Just a thought, but might there be something you can use from the Excel 4/5 macro language? I recall from my various travels around the 'world wide web' that Excel 4 (or 5?) had workbook/sheet events equivalent to those in VBA - OnSheetDeactivate. I don't remember exactly where I read that, and I don't know if there if it is anyway useful, but I can look through my browser history to see where I saw it if you think it might help. Personally, I would love it if the answer was found in an Excel 4 macro - that would just be a quintessentially Excel-y thing to happen.
 
Upvote 0
Hi Jaafar,

Not what you are asking for, the below code just recreates closed window of workbook:
VBA Code:
' Code in ThisWorkbook to catch event one of the workbook's windows has been closed to recreate it

Option Explicit

Dim WnsCount As Long, WnIndex As Long
Dim SelAddress As String

Private Sub Workbook_WindowActivate(ByVal Wn As Window)
 
  If Wn.Parent.Windows.Count < WnsCount Then
   
    MsgBox "Closed window will be recreated"
   
    ' Recreate previously closed window
    Application.EnableEvents = False
    Wn.NewWindow
    With Wn.Parent.Sheets(WnIndex)
      .Activate
      .Range(SelAddress).Select
      '...
    End With
   
    Application.EnableEvents = True
   
  End If
 
End Sub

Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
  WnsCount = Wn.Parent.Windows.Count
  WnIndex = ActiveSheet.Index
  SelAddress = Selection.Address
End Sub
 
Upvote 0
Thank you guys for responding and I am sorry for not acknowledging your replies in good time... I have been having horrible sciatica pain and couldn't even sit on the computer chair
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,332
Members
449,077
Latest member
jmsotelo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top