Thursday, 9 February 2017

ThisWorkbook looks good as a central entry point

So, another successful StackOverflow Bounty today and one where I learnt something.

You'll see my answer evolves. Originally, I gave the COM analogous solution, so an external workbook has classes just like a VB6 project but one cannot use the New keyword, so we have to write something analogous to IClassFactory. Then I gave both a late bound and an early bound aspect to the sample code.

The questioner asked why one was in ThisWorkbook and not the other? I was stumped initially, then I remembered how ThisWorkbook allows extensibility, this is explored in another SO question.

Experimenting with ThisWorkbook, I discovered useful features that the OP wanted and of use to others as well. So placing functions in ThisWorkbook hides them from the user when they enter cell formulae. Also the Subs cannot be accessed using Application.Run so this hides them from the global scope which is surely good practice.

Investigating another SO question (sorry no ref) I chanced upon some MSDN article where some C# VSTO code is called into from VBA. Given this article and the VBA example above it looks like ThisWorkbook is a good central entry point. I recommend.

No comments:

Post a Comment