I used the method of the hidden DLL name space (full credit for that concept
in general goes to Laurent Longre) to make the COM Add-In (CAI) as generic
as possible. I didn't want to require a reference be set to the CAI because
that adds yet another layer of complexity, and the whole scheme will fall
apart if the workbook is closed and Excel remains running. Also, it will
cause problems if the workbook is used on a machine that does have the CAI
installed or loaded. I wanted to avoid that requirement. I wanted the
configuration to be compatible with the circumstance when the CAI was not
present.
The names defined in the DLL Namespace persist even after the workbook that
created them is closed, so a permanent link (as long as Excel is running) is
established with these names. (See also
http://www.cpearson.com/excel/TrulyGlobalVariables.htm for another method of
creating values, Longs only in this case, that persist as long as Excel is
running, regardless of what workbooks are opened and closed. This method is
intended for one workbook to leave a message for another workbook after the
first workbook is closed. The names used in this method are not quite as
"hidden" as the names in hidden DLL namespace -- a user could use the
EnumProps API to see what properties were there and mischievously change a
property value, but that is beyond the capability of all but the most
advanced users. I decided to ignore that shortcoming.)
My only hesitation on using the Hidden DLL Namespace was based on how long
MS is going to support the old XLM language. Its been 12 years since it was
replaced with VBA, so it should be around for a while, but you never know.
MS got rid of Lotus script compatibility in 2002 and dumped VBA for the Mac
the last year (I think -- a Mac guy could confirm that). They may at some
point decide to dump XLM compatibility. The method described on the
TrulyGlobalVariables page is entirely based in Windows API functions, and is
completely independent of Excel (in can be used in any application that
supports VBA or in VB itself), so it is much less likely to be made obsolete
by MS. With the TrulyGlobalVariable procedures, all you need is the handle
of a window that will continue to exist as long as is necessary. You could
even use the Desktop Window in which case the variables would exist as long
as Windows was running, although I'll admit I haven't yet tested that
scenario.
I played around with Events when writing the ExcelShutdown code, but decided
not to use them because it added more complexity than it did utility. I
wanted to keep the CAI as simple and generic (and thus reliable and
flexible) as possible. I'm not really happy that the Namespace names must be
hard coded in both the workbook and the CAI, but since you can't enumerate
through those names, I couldn't think of a viable alternatative. I'm wide
open to suggestions for alternatives.
The VB6 project is really a single file (the Connect dsr file). Using events
and references would have required that the CAI be installed on every
machine that used the Excel workbook. By using the Hidden DLL Namespace, the
code in the workbook will still work (in this context I use the word "work"
to mean "not blow up") if the CAI is not present on the user's machine or is
not loaded. Nothing will happen, of course, if the CAI is not present or
loaded, but there will be no ill side-effects (e.g., missing reference
problems, compiler errors, etc), The macro names loaded into hidden name
space are just strings, and if the CAI is not present or loaded, they will
not be used. There are no side-effect to creating the strings if the CAI is
not present or loaded.
Anyways, that's that. I hope you find it useful. It was an interesting
intellectual exercise to write.
--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)