I have an Excel 2010 macro application that is made up of several workbook files.
FileName (ProjectName)
VFApp.xlsm (VFAppVBA)
VFReportClasses.xlsm (VFReportVBA)
VFImportClasses.xlsm (VFImportVBA)
VFDataClasses.xlsm (VFDataVBA)
ZShared.xlsm (ZSharedVBA)
- Shared is referenced by all the other workbooks.
- Data is referenced by App, Report, & Import.
- Report and Import are both referenced by App.
I’m trying to close all the referenced files when the App file is closed. This works for everything except ZShared.xlsm. For it I get the ‘This workbook is currently referenced by another workbook and cannot be closed.’ error.
The only semi-helpful info I found online was http://support.microsoft.com/kb/211856
and http://support.microsoft.com/default.aspx?scid=kb;en-us;159794. I added some logging to all the classes in ZShared. All instances of it’s classes are getting correctly terminated and I don’t have any workbook objects pointing to ZShared.
I even tried renaming the files like (http://www.allquests.com/question/3996335/Upgrading-Add-ins-from-XL2003-to-XL2007.html).
The weird thing is, if I manually open VFDataClasses.xlsm before opening VFApp.xlsm, then ZShared will close out correctly without error. If I just open VFApp.xlsm and let it open all the other referenced files, I get the above error.
Anybody have any suggestions for figuring out why Excel thinks the workbook is still referenced?
Clairification:
- Each workbook references the others via Tools -> References.
- ZShared consists of public classes and modules with public subs, functions, and constants as well as 3 global variables.
- I’m not, that I know of, doing anything to reference ZShared’s workbooks or other Excel objects.
- The global variables are in ZShared so I don’t have to recreate them in each App that references ZShared. They are handled via public
InitializeGlobalClassesandTerminateGlobalClasses - I added an
m_InstanceIdto every class and debug logging to allClass_InitializeandClass_Terminateso I am 100% certain that all instances of the classes are being terminated correctly.
I wonder if it is possible Excel is creating some hidden reference to the workbook based on using a With statement or some such that isn’t being cleared correctly. On the other hand, it makes no sense that this would only happen if ZShared was not already open when the App started.
I never found an answer to why Excel thinks the file is still referenced. As a work-around I ended up doing 2 things.
If anyone knows of a way to peer into Excel’s internals to see why its reference garbage collection is not working, I’d love to know.