Thursday, December 04, 2008

Office 2003 Addin using Visual Studio Add-in (Extensibility) Project

A while back I had to write an add-in to Microsoft Office 2003 for a client and ran into numerous issues during both the development and the deployment of the add-in. Thought I should post it here in hopes that someone may find it useful.

I use Visual Studio 2005 Standard (I know, I know Visual Studio 2008 is out but I develop Pro/Toolkit and SolidWorks API applications too and am not sure if the upgrade is a simple recompile and link, so have not upgraded.), so in order to develop Microsoft Office 2003 Add-ins I had two options:

  • Use the Visual Studio Add-in Template under the "Extensibility" Project Type.
  • Use Visual Studio 2005 Tools for Microsoft Office 2003 (VSTO)
There is a good comparison of these two choices at CodeProject. I have a few additional points to note here that may influence your choice:
  • Using Visual Studio Add-in Template
    • DEVELOPMENT: Writing code for the add-in gets very clunky. The following is a sample excerpt:
      using Microsoft.Office.Core;
      using nsWord = Microsoft.Office.Interop.Word;

      nsWord.ApplicationClass office_app;

      public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom)
              {
                  applicationObject = application;
                  addInInstance = addInInst;
                  if (connectMode != Extensibility.ext_ConnectMode.ext_cm_Startup)
                  {
                      OnStartupComplete(ref custom);
                  }
                  /// which office app is "application"
                  ///
                  if (application is nsWord.Application)
                  {
                      office_app = (nsWord.ApplicationClass)application;
                      //if (logger.IsInfoEnabled) { logger.Info("host app is Word"); }
                  }
              }

      As can be seen from the last few lines, to determine the Office Application that the add-in is loaded in is an equivalence test (using if-else or switch-case or some similar). I know this is not a good solution, but I don't have anything better (if someone has a better suggestion I would really appreciate the tip.

      Even if I got beyond that I am still casting the 'object application' to 'Microsoft.Office.Interop.Word'. I guess I could have used inheritance to pass 'application' to a sub-class that would deal with the specific Office Application, but I was surprised to find that you could not simply query for the Office App Type.
    • DEPLOYMENT: Deploying the Shared Add-in requires a bunch of updates from Microsoft that have to be applied to the target PC:
I have not had a chance to write code with VSTO, but considering that it was designed for such applications, I am assuming that you will not have most of the weirdness stated here. If you have Visual Studio 2005 Professional, then you can download VSTO here [3]. You can search on Microsoft's Download center for respective versions of VSTO for VS 2008 (for Microsoft Office 2003/2007).

Acknowledgements:
  1. http://social.msdn.microsoft.com/forums/en-US/vsto/thread/10c10162-4841-4b46-9d54-67adfecbf622/
  2. http://msdn.microsoft.com/en-us/library/kh3965hw(VS.80).aspx
  3. http://www.microsoft.com/downloads/details.aspx?FamilyID=8315654b-a5ae-4108-b7fc-186402563f2b&DisplayLang=en

3 comments:

  1. No problem Anish. Do let me know if you prefer more such articles. Nice blog yourself by the way.

    ReplyDelete
  2. shri Ganeshram Iyer..it is really very nice blog.. i m doing my dessartation work of masters degree, in which i probebly use pro/toolkit if i succeed.
    your articals r very useful 2 me as there is very little help is available on this topic.so please keep up the great work..hope u will came up with small application/project using toolkit.........thx again.
    Sandip frm India

    ReplyDelete
  3. Thanks Sandip
    I am not sure if you have seen my articles: Pro/Toolkit + Visual C++ HowTo - I provide a ZIP that has the source code to create a Pro/Toolkit DLL.
    http://ossandcad.blogspot.com/2008/03/howto-example-protoolkit-application.html.

    If you go through my blog archive for 2008, you will find many Pro/Toolkit related posts.

    ReplyDelete