HI,
I got it to work in an add-in but didn't found a solution to quit the application properly...
Add-In "ThisWorkbook" module
Option Explicit
Private MySheetHandler As clsWbEvents
Private Sub Workbook_Open()
Set MySheetHandler = New clsWbEvents
End Sub
Add-In ClassModule called: "clsWbEvents"
Option Explicit
Public WithEvents xlApp As Application
Private Sub Class_Initialize()
Set xlApp = Application
End Sub
Private Sub xlApp_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
Dim vSaveAs
On Error GoTo ErrHandler
If Wb.IsAddin Then Exit Sub
xlApp.EnableEvents = False
xlApp.DisplayAlerts = False
Select Case VBA.MsgBox("Want to save your change to '" & Wb.Name & "' to a file?", vbYesNoCancel + vbExclamation, "Microsoft Excel")
Case vbCancel
Cancel = True
GoTo ErrHandler
Case vbNo
Cancel = True
Wb.Close False
GoTo AppQuit
Case vbYes
vSaveAs = xlApp.Dialogs(xlDialogSaveAs).Show(arg1:=Wb.Name)
Select Case vSaveAs
Case False
Cancel = True
Case Else
Wb.SaveAs Filename:=vSaveAs
Wb.Close False
GoTo AppQuit
End Select
End Select
Err.Clear
ErrHandler:
If Err.Number <> 0 Then
MsgBox "Nr.: " & Err.Number & vbLf & Err.Description
End If
Err.Clear
xlApp.DisplayAlerts = True
xlApp.EnableEvents = True
Exit Sub
AppQuit:
Set vSaveAs = Nothing
Set Wb = Nothing
If xlApp.Workbooks.Count = 0 Then xlApp.Quit
Set xlApp = Nothing
End Sub
Bookmarks