I have a C# helper class (ExcelMacroHelper) that assists with injecting VBA macro code into open Excel workbooks and running the resulting macros. I just realized that the following steps would break my code:
-
C# application injects macro code into the active workbook, which causes the
ExcelMacroHelperto mark its state as ready to run macros -
The user switches to a different workbook in Excel.
-
The C# application attempts to run a macro. The
ExcelMacroHelperwill think it is ready to run macros, but the VBA code was injected into a different workbook so the call will fail.
To fix this, I think I need some way of setting a temporary property on the Workbook object indicating that my macro code has been injected into it, or at least a way of maintaining a list of Workbooks that have been handled. Any ideas?
I ended up using custom document properties. Something like this: