.Net, MS Office, Outlook, Outlook 2007, VSTO

VSTO .NET4 Outlook 2007 Add-in: Referencing Form Regions Programmatically

In this article I intend to further expand on my series of articles covering VSTO development of MS Outlook Add-in components using Visual Studio 2010. In previous articles I have covered things such as adding custom actions to archive email messages via the Application_ItemSend event, as well as adding custom code to save Outlook email messages to a database in the .MSG format.

I will review how to integrate a custom form with events in the Outlook Add-in component. However, I won’t review the GUI aspects of custom form development in Visual Studio since this primarily involves stepping through Wizards and drag and dropping controls to the form UI in Visual Studio. I will assume this is fairly self explanatory and has been covered in depth by Microsoft on it’s support Web site.

I have Created a Custom Form for my Emails… How Can I Reference the Fields?

Microsoft has provided programmers with the Globals class which gives the programmer run time access to Outlook form regions and their contained controls. I think this is pretty cool!

For example I have a custom Outlook form that is displayed with new email messages at the bottom of the message. This is a pretty simple form with just two CheckBoxcontrols, one of which gives the user the option to archive the email when it is sent.

What Exactly Can I use the Globals Class for in MS Office?

The Globals Class supports the following properties:

  • In general, and as part of our example: when working with Outlook, you can reference the application-level project using Globals.ThisAddIn  
  • When working with Outlook you can reference a form region using Globals.FormRegions. In our example we will be using this in combination with the Globals.ActiveInspector property.
  • When working with Word you can reference the document using Globals.ThisDocument
  • When working with Excel you can reference Globals.ThisWorkbook and Sheet#
  • When referencing ribbons in Office use Globals.Ribbons
  • With .NET 4 projects you can use the Globals.Factory property to create ribbon controls, smart tags, and host items.

Sounds Great, but How Can One Programmatically Read the Value of the CheckBox when the User Clicks the Send Button?

Microsoft has two different Objects that can be used to reference a custom form using either the Inspector or the Explorer objects.

  • The Inspector object is the window in which the Outlook email is displayed.
  • The Explorer object is a window in which the contents of a folder are displayed (like Windows explorer, or the Outlook mail list).

So to reference the custom form in our new email message we need to use the Inspector object. Here is an example of how one can grab the state of a CheckBox control in the Application_ItemSend event of an email:

Private Sub Application_ItemSend(Item As Object, ByRef Cancel As Boolean) Handles Application.ItemSend
Dim mailItem As Outlook.MailItem = TryCast(Item, Outlook.MailItem)
If (mailItem IsNot Nothing) Then
Dim formRegions As WindowFormRegionCollection = Globals.FormRegions(Globals.ThisAddIn.Application.ActiveInspector())
Dim boolSaveToDb As Boolean = formRegions.MyCustomForm.cbSaveThisEmailToDb.Checked
End If
End Sub

So all in all, The Globals class is quite straightforward and powerful. The trick (as with any programming really) is just knowing about the object and getting the syntax right.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s