C
carlos_almeida
I already posted about this subject but didn't understand how to
implement.
I have the following code:
In a Class Module named EventClass
-----------------------------------------------
Option Explicit
Public WithEvents App As Application
Private Sub App_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As
Boolean)
If Cancel = True Then Exit Sub
Application.EnableEvents = False
For Each Wb In Workbooks
If Wb.Saved = False And Not Wb.IsAddin Then
Wb.Activate
Select Case MsgBox("Do you want to save changes made to '"
& _
Wb.Name & "'?", vbExclamation + vbYesNoCancel +
vbDefaultButton1, "MyFile")
Case vbCancel
Cancel = True
Application.EnableEvents = True
Exit Sub
Case vbNo
Wb.Saved = True
Case vbYes
If Wb.Name = ThisWorkbook.Name Then
DadosIncompletos 'Check if there is data to fill
If bClose Then 'bClose as boolean
Select Case MsgBox(" You must fill all data in
'" _
& Wb.Name & "'. " & Chr(10) & " Do you want to
save? ", _
vbExclamation + vbOKCancel, "MyFile")
Case vbCancel
Cancel = True
Application.EnableEvents = True
Exit Sub
End Select
End If
End If
Wb.Saved = False
End Select
End If
Next Wb
For Each Wb In Workbooks
If Wb.Saved = False And Not Wb.IsAddin Then
If Wb.Name = ThisWorkbook.Name Then
Wb.Save
Application.ScreenUpdating = False
HideSheets 'Sub to hide all sheets, except the warning
sheet
Application.StatusBar = " Processing " &
ThisWorkbook.Name & "..."
Wb.Save
UnHideSheets
Application.StatusBar = False
Application.ScreenUpdating = True
Else
If Wb.Path = "" Then
Cancel = Not
Application.Dialogs(xlDialogSaveAs).Show
Else
Wb.Save
End If
End If
End If
Wb.Saved = True
Next Wb
Application.EnableEvents = True
End Sub
In ThisWorkbook Module
--------------------------------
Option Explicit
Dim AppClass As EventClass
Private Sub Workbook_Open()
Set AppClass = New EventClass
Set AppClass.App = Application
Application.WindowState = xlMaximized
'More code...
end sub
This code works ok if I have only one workbook open (ThisWorkbook).
But if I have more books open then I have problems... The workbooks
don't close or don't save properly...
Mr.Chip Pearson told me that "I needed to use the WorkbookBeforeClose
Application event to detect when other workbooks close". Thanks
Mr.Chip but I'm still new in VBA and haven't figured out how to do
that.
I'm working on this for about 4 weeks and this problem with
App_WorkbookBeforeClose as well with App_WorkbookBeforeSave is making
me crazy...
I kindly ask if someone could explain me and give me an example so I
could learn how to do that. I think that I'm near the solution but
still can't see.
Many thanks and forgive me my bad English (my Portuguese is mutch
better).
Carlos Almeida
implement.
I have the following code:
In a Class Module named EventClass
-----------------------------------------------
Option Explicit
Public WithEvents App As Application
Private Sub App_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As
Boolean)
If Cancel = True Then Exit Sub
Application.EnableEvents = False
For Each Wb In Workbooks
If Wb.Saved = False And Not Wb.IsAddin Then
Wb.Activate
Select Case MsgBox("Do you want to save changes made to '"
& _
Wb.Name & "'?", vbExclamation + vbYesNoCancel +
vbDefaultButton1, "MyFile")
Case vbCancel
Cancel = True
Application.EnableEvents = True
Exit Sub
Case vbNo
Wb.Saved = True
Case vbYes
If Wb.Name = ThisWorkbook.Name Then
DadosIncompletos 'Check if there is data to fill
If bClose Then 'bClose as boolean
Select Case MsgBox(" You must fill all data in
'" _
& Wb.Name & "'. " & Chr(10) & " Do you want to
save? ", _
vbExclamation + vbOKCancel, "MyFile")
Case vbCancel
Cancel = True
Application.EnableEvents = True
Exit Sub
End Select
End If
End If
Wb.Saved = False
End Select
End If
Next Wb
For Each Wb In Workbooks
If Wb.Saved = False And Not Wb.IsAddin Then
If Wb.Name = ThisWorkbook.Name Then
Wb.Save
Application.ScreenUpdating = False
HideSheets 'Sub to hide all sheets, except the warning
sheet
Application.StatusBar = " Processing " &
ThisWorkbook.Name & "..."
Wb.Save
UnHideSheets
Application.StatusBar = False
Application.ScreenUpdating = True
Else
If Wb.Path = "" Then
Cancel = Not
Application.Dialogs(xlDialogSaveAs).Show
Else
Wb.Save
End If
End If
End If
Wb.Saved = True
Next Wb
Application.EnableEvents = True
End Sub
In ThisWorkbook Module
--------------------------------
Option Explicit
Dim AppClass As EventClass
Private Sub Workbook_Open()
Set AppClass = New EventClass
Set AppClass.App = Application
Application.WindowState = xlMaximized
'More code...
end sub
This code works ok if I have only one workbook open (ThisWorkbook).
But if I have more books open then I have problems... The workbooks
don't close or don't save properly...
Mr.Chip Pearson told me that "I needed to use the WorkbookBeforeClose
Application event to detect when other workbooks close". Thanks
Mr.Chip but I'm still new in VBA and haven't figured out how to do
that.
I'm working on this for about 4 weeks and this problem with
App_WorkbookBeforeClose as well with App_WorkbookBeforeSave is making
me crazy...
I kindly ask if someone could explain me and give me an example so I
could learn how to do that. I think that I'm near the solution but
still can't see.
Many thanks and forgive me my bad English (my Portuguese is mutch
better).
Carlos Almeida