Monday, February 14, 2005

So after my last idea, I am going to stick with a little more technical post for the time being.  Let’s start with Microsoft Office.

With .NET, Microsoft has taken a step forward to make it a little more easy to program with office by using the primary interop assemblies (PIAs).  They have even gone through the trouble to give you examples for alot of situations. This is a wonderful start to working with Office.  The problem comes up when you write your application for one version of Office, say Office2003, but someone else has Office XP or even worse Office2000.  So now you are stuck, do you write another application just for Office XP or Office 2000, or do you try to make one application that works for the Office versions you need?  I guess the best approach would be to use late binding for the entire Office interop.

Well I think this is where Microsoft came up short on the programming API you use to program with Office.  And yes I know Office is still written in unmanaged code.  I think Microsoft should have distributed an Interop for Office not for a specific version of office, but to program against Office.  This way you wouldn’t have to write a program for multiple versions of the PIAs.

Here is what my code idea would look like:

public object StartExcel
{
  switch(offieversion)
  {
   case Office2003:
          // Instantiate Excel using PIAs from Office2003.
          objApp = new Excel.Application();
     break;
   case OfficeXP:
          // Instantiate Excel using PIAs from OfficeXP.
          objApp = new Excel.Application();
     break;
   case default:
          // Get the class type and instantiate Excel.

          Type objClassType;
          objClassType = Type.GetTypeFromProgID("Excel.Application");
          _Excel = Activator.CreateInstance(objClassType);
     break;
  }
}

Now you would have to do this for every function that is exposed to office, but then you would have the ability to use any version of office and use the best suggested method to do so.  Why does Microsoft make it so difficult to program against office.  It is not that hard, but it is not always as straightforward as you might think it could be.  If you try to program with LateBinding to office objects you cannot find a good reference to show you the method calls or properties that you need.

Let me know what other people have tried in their applications to get around this problem.

—Brendon Schwartz

 

 

2/14/2005 12:40:13 PM (Eastern Standard Time, UTC-05:00)  #    Trackback