I am building a COM add-in for Excel 2007+ which will be an application in the sense that:
- It has its own tab on the Ribbon with buttons that control its operation (such as "create new planning workbook"/"refresh workbook" etc.)
- It will trap events from Excel (both Application and Workbook) and act upon them.
- It should have a task pane which will be context-based, and will have data sent to it from my addin.
In order to support this I would like to structure the code in such a way that I can modify it relativley easily in the future (for example, adding a new button on the Ribbon) and not have spaghetti everywhere.
My initial thoughts were to create a singleton class which would be my "Addin Application" and initialise this with the Excel application itself, for example:
public class Connect : Extensibility.IDTExtensibility2 {
private Excel.Application _excelApplication;
private MyAddinApplication _myAddinApplication;
public void OnConnection(Object application,...) {
_excelApplication = (Excel.Application)application;
_myAddinApplication = new MyAddinApplication(_excelApplication);
}
}
This MyAddinApplication class would then trap Excel's events (such as opening a workbook, closing etc.) and then act on them accordingly. It would also trap any Ribbon events or callbacks and then trigger actions based on a Command pattern.
My question, is this a reasonable approach for an Excel COM addin? Would it be better to encapsulate the Excel application in a separate "event handler" class which is referenced by my "Addin Application" class? I've not really seen a complex COM addin from my research, only ones which have one or two buttons or do not trap any events.