Question

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.

Was it helpful?

Solution

Here is one (a bit more complex) Excel addin you can look at. The author describes how he organized his solution into different projects and I agree with most of his decisions. He separates the domain logic from the user interface / excel and also adds unit testing project and one project that bootstraps his whole addin:

http://www.clear-lines.com/blog/post/VSTO-solution-organization.aspx

Source code is available here: http://vstostocks.codeplex.com/

This is a VSTO solution but you can easily apply the same principles to a plain COM addin. It also quite similar to your project.

It is hard to give guidelines without knowing specifics of you problem. I think if you stick to main guidelines when it comes to OO design like SOLID, writing clean code, you should be just fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top