Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2008 Office Interop 2003 vs 2007

Tags:

c#

.net

vsto

I am developing an add-in for Microsoft Excel, using Visual Studio .NET 2008.

The add-in creates a single toolbar button, which can be clicked to launch a form, which can be used to add values from a database into the cells of the active spreadsheet.

1) A requirement is that the tool be available in both Excel 2003 and in 2007.

2) Another requirement is that, in Excel 2007, the launch button be on its own ribbon tab.

Because of the ribbon tab requirement, I have created two separate Excel add-in projects within Visual Studio - one for each version of office.

However, because the two add-ins must reference two different Office.Interop assemblies, and the project providing the database query form can only reference one, I find myself unable to share this third assembly between the two add-in projects.

Does anybody have a simpler solution than maintaining a separate copy of the form code for each of the two add-in versions?

Thanks.

like image 538
mcoolbeth Avatar asked Oct 14 '22 09:10

mcoolbeth


1 Answers

There are two options:

Option 1: Don't reference any Interop assemblies in the shared project providing the database query. Use interfaces and dependency injection to provide the required Excel interoperability code from the add-in projects.

Let me give you an example: Assume that your shared project needs to execute some function func which needs access to the Interop libraries. You could create an interface in your shared project:

public interface ExcelInterface {
    void func();
} 

In your add-in projects, your provide implementations for this interface:

class Excel2003Interface : ExcelInterface { // located in your Excel 2003 Addin
    void func() {
        // the code here can use the Excel 2003 interop reference
    }
}

Similarly, you create Excel2007Interface in your Excel 2007 addin.

Then, when opening the form in the shared project, you pass an instance of either Excel2003Interface or Excel2007Interface, which is used by the form to call func:

void DoSomething(ExcelInterface iface) {  // this is in your shared project
    ...
    iface.func();
    ...
}

Option 2: Use the litte-known link file feature of Visual Studio to share code between the two add-in projects.

like image 89
Heinzi Avatar answered Oct 28 '22 19:10

Heinzi