?
Solved

BeforeClose triggered from event in this workbook

Posted on 2013-05-14
4
Medium Priority
?
324 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 2000 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Ever wonder what it's like to get hit by ransomware? "Tom" gives you all the dirty details first-hand – and conveys the hard lessons his company learned in the aftermath.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
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…

777 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