Mysterious Errors When Using Form Regions from Managed Code Add-ins

I've been noticing some feedback lately where a number of developers were having problems working with the controls on their form region.  Usually, these problems manifested as Outlook's object model returning null values back when trying to access the Controls collection of the UserForm object in the Microsoft Forms 2.0 library (which was accessed from the FormRegion.Form property).

This can be especially frustrating, because it looks like you've done everything right, and Outlook is just giving you back something that is totally wrong. Turns out, that isn't quite the case.

If you've added a reference to the 'Microsoft Forms 2.0' type library, you might have added the wrong reference to your project. On some computers, you'll find two different listings for the forms type library in the Add Reference dialog:

Depending on which reference you select, you'll either get a reference properly associated with the Primary Interop Assembly provided by Office, or you'll get a reference with a Visual Studio generated interop assembly. Unfortuatnely, the VS generated interop assembly incorrectly maps some elements of the type library to elements in the interop assembly.  This ultimately means that when you are using this reference, you'll end up calling into a different property or method than your code appears to be calling, which causes various strange exceptions and errors.

You can easily determine which reference you are using by selecting the reference and looking at the properties of the reference. If you've added the self-generated IA reference, the properties will look like this:

Notice that the reference has "Copy Local" set to True, and that the Path is not pointing at the Global Assembly Cache (GAC) but rather to the location of my solution.  This indicates that I'm using a self-generated interop assembly, and that I may be running into issues.

If you've added the correct reference to the PIA, then you'll see properties that look like this instead:

This time around, you'll see the Path is pointing into the GAC, and the "Copy Local" property is False. This indicates the PIA is being used, and everything should work normally.

If you find your project is using the incorrect reference, you should remove the reference and add a new reference.  Unforutnately, there is no way to determine which reference is the "right" reference from the Add References dialog.  You'll simply need to use trial and error and double check after adding the resource to make sure you've picked the right one.

The Take-away: Make sure you are using the right interop assembly when working with custom forms or form regions from your managed code solution.  Using the Visual Studio generated interop assembly can cause mysterious errors in your solution that you may not catch right away.

Comments

  • Anonymous
    August 02, 2007
    I have been getting this problem even after setting the right reference to Microsoft Forms 2.0 library. I am using a ListBox control on my form region and want to get a handle to it in my form region code. I am doing something like below. public void BeforeFormRegionShow(Outlook.FormRegion FormRegion)        {            this.FormRegion = FormRegion;           this.FormRegion.SuppressControlReplacement = true;            this.UserForm = FormRegion.Form as UserForm;            Microsoft.Vbe.Interop.Forms.ListBox list1 = this.UserForm.Controls.Item("Notes") as Microsoft.Vbe.Interop.Forms.ListBox; But I am getting the value of list1 as null. Is there anything else that I am missing here ?? Thanks Ram

  • Anonymous
    August 03, 2007
    The comment has been removed

  • Anonymous
    August 07, 2007
    The comment has been removed

  • Anonymous
    September 04, 2007
    As you said, I have tried the interface Outlook._FormRegionStartup. But it always can't invoke the method public void BeforeFormRegionShow(Outlook.FormRegion FormRegion). I don't konw what's reason.