2011-07-05 27 views

回答

0

看看這個文件: http://support.microsoft.com/kb/213566

創建模塊和類模塊。代碼如下。

類模塊代碼:

Public WithEvents appevent As Application 

Dim windowsCount As Integer 


Private Sub appevent_WindowActivate(ByVal Wb As Workbook, ByVal Wn As Window) 
If windowsCount <> Application.Windows.Count Then 
    MsgBox "You closed a window" 
End If 
End Sub 


Private Sub appevent_WindowDeactivate(ByVal Wb As Workbook, ByVal Wn As Window) 
windowsCount = Application.Windows.Count 
End Sub 

模塊代碼:

Dim myobject As New Class1 


Sub Test() 

Set myobject.appevent = Application 

End Sub 

而且此處理工作簿:

Private Sub Workbook_Open() 
Test 
End Sub 
+0

謝謝安德森,我會嘗試看看能否以這種方式實現它。因爲我在功能區中添加了一個按鈕,所以用戶必須使用按鈕而不是窗口上的X按鈕來關閉窗口,這樣我才能夠運行所需的自定義邏輯。不是理想的解決方案,但這是迄今爲止我所能做到的。 –

-1

您可以處理BeforeClose事件。使用VBA的示例可用here

+1

弗雷德裏克你好,謝謝你的迴應。 Workbook.BeforeClose事件僅在上一個工作簿窗口關閉時觸發。我需要的是處理Workbook窗口關閉事件。因此,如果您有Book1.xlsx:1和Book1.xlsx:2,那麼我需要知道Book1.xlsx:2何時關閉。 –

相關問題