Solved

BeforeClose triggered from event in this workbook

Posted on 2013-05-14
4
321 Views
Last Modified: 2013-05-16
When I put the BeforeClose routine in Thisworkbook module it works OK, but I wanted to run it from a class module. Now it completely ignores the beforeclose routine. My reason is that if later changes are made to the close routine in various workbooks, it will then just be a matter of importing a new class module.

The following is my code:
-  - -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -

'   EventsMod1
'   last updated 01/04/2013

Option Explicit

Private WithEvents App As Application

'==============================================
Private Sub Class_Initialize()

    Set App = Application
End Sub
'===================================================
Sub Workbook_BeforeClose(Cancel As Boolean)
   
    Application.ScreenUpdating = True
    Application.AutoRecover.Enabled = True
       
    If FMVarMod.FMGUseNoTimer = False Then
      If FMVarMod.FMGAllignTimer(1) <> -2 Then
        FMVarMod.FMGAllignTimer(1) = -1
        FMDrawMod6.AllignTimerSub     'killing API timer
      End If
    End If
       
    ThisWorkbook.Save

End Sub
'===============================================



'==========================================
'CODE IN THISWORKBOOK MODULE
'Option Explicit

'Dim FMEventsMod1 As New EventClassModule

'================================================
'Private Sub Workbook_Open()
'    Set App = Application
'    InitializeApp
   
'End Sub
'===========================================
'Sub InitializeApp()
'    Set FMEventsMod1.App = Application
'End Sub
'=============================================

Can someone please point out what I am doing wrong?
0
Comment
Question by:mellenf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 500 total points
ID: 39167196
In order to use Workbook events in a normal class module you will need to declare a WithEvents variable of type Workbook, and assign the correct workbook to it, then you will be able to assign the event proc with the correct name.  This just uses the activeworkbook, but you could modify the code to assign any workbook to the variable - this is the workbook that will fire the events.

Option Explicit

Private WithEvents App As Application
Private WithEvents Wbk As Workbook

'==============================================
Private Sub Class_Initialize()

    Set App = Application
    Set Wbk = App.ActiveWorkbook

End Sub
'===================================================
Private Sub Wbk_BeforeClose(Cancel As Boolean)
    
    Application.ScreenUpdating = True
    Application.AutoRecover.Enabled = True
       
    If FMVarMod.FMGUseNoTimer = False Then
      If FMVarMod.FMGAllignTimer(1) <> -2 Then
        FMVarMod.FMGAllignTimer(1) = -1
        FMDrawMod6.AllignTimerSub     'killing API timer
      End If
    End If
        
    ThisWorkbook.Save

End Sub
'===============================================

Open in new window

0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 39168593
Small amendment - line 26 should be
Wbk.Save

Open in new window


:)
0
 
LVL 17

Expert Comment

by:andrewssd3
ID: 39169142
Quite right, of course.  It would be especially necessary if you wanted to have this code in say a global add-in with the variable Wbk passed in from another workbook.

It would also be good practice to change the Application. references in the Before_close routine to App. This would give you scope to pass in the application too if you wanted to be using more than one instance of Excel.

Thanks
0
 

Author Closing Comment

by:mellenf
ID: 39170657
thank you andrewssd3.
This was so simple, but it worked.
mellenf
0

Featured Post

Office 365 Advanced Training for Admins

Special Offer:  Buy 1 course, get 2nd free!  Buy the 'Managing Office 365 Identities & Requirements' course w/ Accelerated TestPrep, and automatically receive the 'Enabling Office 365 Services' course FREE!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question