Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / January 2008

Tip: Looking for answers? Try searching our database.

Capture Excel close

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Keith74 - 24 Jan 2008 16:40 GMT
Hi All

I've got the following code in the "ThisWorkbook" code object.

Option Explicit
Public WithEvents App As Application

Private Sub App_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As
Boolean)

Cancel = False

If Left(Wb.Name, 4) <> "Book" Then
   MsgBox "beep"
   Cancel = True
   Exit Sub
End If

End Sub

Public Sub SetApp()

   Set App = Application

End Sub

and in another module

ThisWorkbook.SetApp

the problem i'm having is that the "App" object clears as soon as
focus returns to the calling module. Anyone have any idea why, i'm at
a complete loss on this one.

cheers

Keith
Jim Thomlinson - 24 Jan 2008 17:22 GMT
Your a little off in how you have set this up. The gist of what you want is
to instantiate an object of your class. You are not using a class module. you
put the code in this workbook whcih will not work. Add a class module to your
project. That object will contain an instance of XL which listens for XL
events. So you are close but you need to make a couple of tweaks...

Add a class Module and name it something like clsXLEvents
Put your code into that  class.

When you instantiate an object of your class you want to create the XL
instance so use the Class Initialization event something like this...

Private Sub Class_Initialize()
   Set App = Application
End Sub

Instead of
Public Sub SetApp()
   Set App = Application
End Sub

Now you just need to create an instance of your class something like this...
In a standard module publicly declare an object of type clsXLEvents

public AppEvents as clsXLEvents
Somewhere in code (ususally in the open event of this workbook) add
set AppEvents = new clsXLEvents

Check out this link for more info on application level events...
http://www.cpearson.com/excel/AppEvent.aspx

Signature

HTH...

Jim Thomlinson

> Hi All
>
[quoted text clipped - 33 lines]
>
> Keith
Keith74 - 24 Jan 2008 17:28 GMT
Thanks Jim, i'll give that a go
Robert Bruce - 25 Jan 2008 14:52 GMT
Yn newyddion: AC779F6A-F323-4BC0-8F24-7F129F6EF3F0@microsoft.com,
Roedd Jim Thomlinson <James_Thomlinson@owfg-Re-Move-This-.com> wedi
ysgrifennu:

> You are not using a
> class module. you put the code in this workbook whcih will not work.

ThisWorkbook /is/ as class module. I just tried the OP's code and it worked
just fine. After running Thisworkbook.setapp, "?thisworkbook.App is nothing"
in the immediate window returns false, indicating that the App variable
still refers to the Application object.

Rob
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.