View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Wei-Dong Xu [MSFT] Wei-Dong Xu [MSFT] is offline
external usenet poster
 
Posts: 120
Default ThisWorkbook.close doesn't wokk :(

Hi Arnaud,

Thank you for the detailed information about the repro!

From my view, you code will work very well for one workbook-scope workbook_beforeclose event. However, you are using them in one add-in
which has the application scope. This add-in can't locate which workbook to perform the operation even there is only one. This is the reason why
you get the crash or find some odd behavior. I'd suggest you can change the code from the original to the one below, adding one loop to search
the corresponding workbook and retrieve the information for your scenario:
'Code begin ----------------------------------------------------
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim wbk As Workbook
For Each wbk In Application.Workbooks
'you can specify your own condition
' for example, you can also check your own CloseLater here.
If wbk.Saved = False Then
'perform some operations
....
'in sample, save the workbook directly
wbk.save
End If
Next
End Sub
'Code end ------------------------------------------------------

Please feel free to let me know if you have any further questions. I am standing by to be of assistance.

Have a nice day!

Wei-Dong Xu
Microsoft Product Support Services
Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.