Solved

BeforeClose triggered from event in this workbook

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

This article will show you how to use shortcut menus in the Access run-time environment.
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
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…

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