LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Handling the WorkbookAfterSave Event in VBA

Thanks very much, Chip. I think I may be able to adapt your suggestion to my
requirement. However, since you asked for a clarification -- and since I
need all the help I can get -- here it is.

My original post simplified my problem somewhat. I thought that would make
it easier to respond, and I could extract the info I need from that
response. Here's a fuller description.

I wrote a VB 2008 exe which instantiates Excel & reads a workbook. Lets call
this the "maaster workbook". This master workbook includes code like the
following:

Private mCCalc As Object

Public Sub Workbook_Open()

'Open a DLL written in VB 6.
Set mCCalc = CreateObject("SomeName", "")

'Pass the DLL a reference to the Excel application.
mCCalc.Init Application

End Sub

Later the DLL creates & manipulates new workbooks. Because there can be a
lot of them (e.g. 1,000 workbooks), it pages some of them out to disk and
removes them from RAM (ie. from the Workbooks collection) to save Excel
resources when it isn't using them. It then reads them back when it needs
them, perhaps paging other workbooks to disk. Unfortunately, although the
program tries to predict which workbooks won't be needed soon, it cannot do
this accurately. Therefore, a workbook might be written, deleted, and read
in quick succession.

This write/delete/read cycle can crash Excel. I believe that this is caused
by reading a workbook before the last save is finished. My reason for this
belief is that adding a 5 second time delay before reading each workbook
prevents the crashes. However, a fixed time delay isn't satisfactory
because: (1) I don't know if 5 seconds will always be enough. 1 seconds is
too short. (2) The 5 second delay makes the program run too slowly (i.e.
hours, perhaps days. I gave up waiting) and usually isn't necessary.

Therefore, I thought I'd replace the fixed time delay with a variable delay
loop which exits when the save finishes. I could make a collection of the
names of the workbooks I'm saving and remove names from that collection in a
WorkbookAfterSave event handler.

Thus, the natural place to handle theWorkbookAfterSave event would be the VB
6 DLL, where all this saving and reading is going on. Since
the master workbook always stays open and always keeps its initial reference
to the DLL, the DLL stays open too. Alternatively, I could add it to VBA in
the master workbook. Since I couldn't figure out how to do either of these
things (although perhaps I can now, with your suggestions), I tried handling
the WorkbookAfterSave event in the VB 2008 exe that started everything, but
couldn't get that working because I couldn't get VB to recognize the
reference to AppEvents_WorkbookAfterSaveEventHandler and AppEvents_Event.
(Yes, I know that that should be the easy part.) If I can catch the event
anywhere, I can pass the information to the point at which it's needed, but
the VB 6 DLL would be the best place to put it.

Well, that's probably a longer answer than you expected. I hope I'm not
wearing out my welcome. Anyway, I appreciate the info you've already
provided.

Thanks again.

"Chip Pearson" wrote in message
...
I somewhat misread your post. My previous reply is of limited
relevance. Where are you putting the

Private WithEvents ExcelApp As Application

declaration and where are you initializing the ExcelApp variable? The
code I posted in my previous reply works as expected, so it might not
be completely useless. In that code, I use a separate class module to
handle the XLApp and its events, but this code could be placed
directly in ThisWorkbook if desired.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional,
Excel, 1998 - 2010
Pearson Software Consulting, LLC
www.cpearson.com



On Sun, 16 May 2010 15:00:33 -0400, "Steve Flaum"
wrote:

I can handle the Excel WorkbookAfterSave event in VB 2010 as explained at
http://msdn.microsoft.com/en-us/libr...ffice.14).aspx.
I
can also handle built-in events of the Excel application in VBA as
described
at http://msdn.microsoft.com/en-us/libr...ffice.10).aspx.
However, I cannot figure out how to handle the WorkbookAfterSave event in
VBA. If I use a statement such as:

Private WithEvents ExcelApp as Application

a number of application events are available, but WorkbookAfterSave isn't
one of them.

I suppose I could create a VB 2010 component which catches and rethrows
the
event, but there must be a better way. Can anyone tell me what it is?

Thanks.


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Handling Print Preview Event Corey C. Excel Programming 0 May 16th 08 08:04 PM
checkbox event handling function nevwalters Excel Worksheet Functions 2 May 16th 06 04:27 AM
Changing RightClick event handling GeyserPeak[_2_] Excel Programming 1 July 27th 05 04:39 AM
Event handling... Alex Excel Programming 1 January 29th 05 06:24 PM
excel object event handling Vladimir Kryachko Excel Programming 1 September 9th 04 08:01 AM


All times are GMT +1. The time now is 09:24 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"