Solved

BeforeClose triggered from event in this workbook

Posted on 2013-05-14
4
317 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
  • 2
4 Comments
 
LVL 17

Accepted Solution

by:
andrewssd3 earned 500 total points
Comment Utility
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
Comment Utility
Small amendment - line 26 should be
Wbk.Save

Open in new window


:)
0
 
LVL 17

Expert Comment

by:andrewssd3
Comment Utility
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
Comment Utility
thank you andrewssd3.
This was so simple, but it worked.
mellenf
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now