2011-07-05 34 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何时关闭。 –

相关问题