takeyato / excel

0 stars 0 forks source link

WorkbookClosedDetector #1

Open takeyato opened 2 hours ago

takeyato commented 2 hours ago

' クラスモジュール: WorkbookClosedDetector Option Explicit

Public Event WorkbookClosed()

Private WithEvents App As Application Private cnt As Long

Private Sub Class_Initialize() Set App = Application cnt = Workbooks.Count End Sub

Private Sub App_WorkbookOpen(ByVal Wb As Workbook) cnt = Workbooks.Count End Sub

Private Sub App_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean) If Workbooks.Count - 1 = cnt Then RaiseEvent WorkbookClosed End If End Sub

takeyato commented 2 hours ago

' 標準モジュール Dim detector As WorkbookClosedDetector

Sub InitializeDetector() Set detector = New WorkbookClosedDetector AddHandler detector.WorkbookClosed, AddressOf WorkbookClosedHandler End Sub

Sub WorkbookClosedHandler() MsgBox "他のブックが閉じられました。" End Sub