Solved

BeforeClose triggered from event in this workbook

Posted on 2013-05-14
4
322 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

This article describes how to import an Outlook PST file to Office 365 using a third party product to avoid Microsoft's Azure command line tool, saving you time.
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

717 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