Saturday, November 14, 2009

Deploying an Outlook 2003 custom form along with VSTO add-in

You can design a form using Microsoft Outlook form designer (Tools -> Forms -> Design a form). The form is saved as the template (.oft) file. An outlook user can design and set a custom form as his default email composing form by publishing the form.

When we need to use / access the custom form in VSTO plug-in code, we need to publish this form on end user's machine first and on that machine, make this form default for composing the new mails.

When publishing a custom form, we need to give it a class name, that distinguishes our form. When deploying on end user's machine, we need to tell outlook the form class name (our form class set during publishing on user's machine) to use for composing the emails.

I followed the following approach for publishing and deploying the custom Outlook 2003 form with VSTO add-in :
  1. Design the custom form as required in Outlook form designer.
  2. Add a custom action in the add-in installer to make registry entries in order to make our form default composing form :
    We need to set our form's class name (say the class name is "MyCustomForm") in the following registry entry :

    Registry key:

    HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Outlook\Custom Forms\Compose

    Value name : IPM.Note
    Value type : binary (in our case, our form class name is MyCustomForm, so we will need to convert this string to binary and set this binary as the value of IPM.Note)

    We need to set the class "IPM.Read" as the default form class for reading the email messages so that our form should not be used for reading the email messages (if we have the form only for composing the emails). We will set this registry entry :

    HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Outlook\Custom Forms\Compose

    Value name : IPM.Note
    Value : IMP.Read (need to convert it to binary first, have given the code later)
    Value type : binary

    Below is the code that sets the all above registry entries :

    string clsName = "IPM.Note.MyCustomForm";
    byte[] bytes = new byte[clsName.Length + 2];
    //Convert to binary
    for (int i = 0; i < bytes.Length - 2; i++)
    {
    bytes[i] = (byte)clsName[i];
    }

    bytes[bytes.Length - 2] = 0;
    bytes[bytes.Length - 1] = 0;
    Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Outlook\Custom Forms\Compose", "IPM.Note", bytes, RegistryValueKind.Binary);

    //Set default reading form to be the outlook's default form for reading / opening messages.

    clsName = "IPM.Read";
    bytes = new byte[clsName.Length + 1];
    for (int i = 0; i < bytes.Length - 1; i++)
    {
    bytes[i] = (byte)clsName[i];
    }

    bytes[bytes.Length - 1] = 0;
    Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Office\11.0\Outlook\Custom Forms\Read", "IPM.Note", bytes, RegistryValueKind.Binary);
    }//try
    catch (System.Exception exception)
    {
    EventLog.WriteEntry("ILC Outlook 2003", "Error making custom form entries : " + exception);
    }

    We need to publish our form with class name as we set in the registry entry above for compose.
  3. Publish the form with class name e.g. in our case MyCustomForm.
  4. Access form's controls in the add-in code to make it function along with the add-in :

    e.g. Accessing a list box in add-in code :

    //Add reference to Microsoft.Interop.Vbe.Forms assembly
    //Add using statement as MSF= Microsoft.Interop.Vbe.Forms
    //Suppose you have modified the "Message" tab of the outlook custom form

    Inpector Inspector = Application.ActiveInspector();
    MSF.UserForm usf = (Inspector.ModifiedFormPages as Outlook.Pages)["Message"] as MSF.UserForm;
    MSF.ListBox lstAttachment = usf.Controls.Item("lstAttachments") as MSF.ListBox;
    // now you can use lstAttachment in your code anywhere.

No comments:

Post a Comment

Love to have comments from you !