VSTO for MS Outlook 2007 – Using msocontroledit and msoControlComboBox in the Commandbars

Outlook Custom Search Fields
Outlook Custom Search Fields

VSTO for MS Outlook can be a powerful way to customize Outlook programmatically. However, the syntax can be finicky at times and I have found a few instances where the programming logic becomes tricky.

For example, you can use VSTO to add a text box to the Outlook command bar window. This can be put to a number of good uses when building a custom component for Outlook. My thoughts behind adding the controls to Outlook were to allow the user to select a type of search to run in the custom dropdown list, and then to use the text box for the user to add text that the system would use to run a search on custom email fields.

In my case I wanted to add a textbox and a ComboBox to my main Outlook Explorer view. The VSTO controls in Outlook are called msoControlEdit and msoControlComboBox respectively.

At first glance adding a simple text box and dropdown list sounds like a simple requirement. But as I found out, this isn’t quite the case. The problem? Outlook by default will clear any entered text from the new text box when focus is lost. I’m not sure what the reasoning behind this is, but from reading help articles  and forum posts, it appears that many people are coming up against the same problem.

As a positive, the ComboBox control does retain selected text, so we will not need to worry about handling this control.

For the convenience of readers of this article; I am including a full example of how to add a textbox and a ComboBox  to the Outlook main command bar at the end of this article.

How can one get Outlook to keep text from a textbox when the focus is lost?

The answer lies in adding a timer control from the Windows.Forms library. The timer needs to tick at a reasonable rate and then store newly entered text into a member variable in the ThisAddIn class. I did spend some time checking if this was the only solution, and in fact it is.

In the example code below I have set the timer to tick at once per second. This is frequent enough to catch text changes while not consuming too many clock cycles so that the application performance is still smooth.

Imports Outlook = Microsoft.Office.Interop.Outlook
Imports Office = Microsoft.Office.Core
Imports System.Windows.Forms
Public Class ThisAddIn
Dim WithEvents edt As Office.CommandBarComboBox
 Dim WithEvents editor As Office.CommandBarComboBox
 Dim WithEvents timer As New Timer()
 Dim strSearchText As String = Nothing
Private Sub ThisAddIn_Startup() Handles Me.Startup
 Dim cbStandard As Office.CommandBar = Nothing
 cbStandard = Application.ActiveExplorer().CommandBars("Standard")
 timer.Interval = 1000
 timer.Enabled = True
 editor = CType(cbStandard.FindControl(Tag:="Search Box", Visible:=True, Recursive:=False), Office.CommandBarControl)
 If editor Is Nothing Then
 editor = cbStandard.Controls.Add(Type:=Office.MsoControlType.msoControlEdit, Temporary:=True)
 editor.Tag = "Search Box"
 editor.Caption = "Use this text area to search for Information in your folder."
 End If
 edt = CType(cbStandard.FindControl(Tag:="DDL", Visible:=True, Recursive:=False), Office.CommandBarControl)
 If (edt Is Nothing) Then
 edt = cbStandard.Controls.Add(Type:=Office.MsoControlType.msoControlComboBox, Temporary:=True)
 edt.Caption = "Use this search area to specify the search to run for emails in your folder."
 edt.Tag = "Search Text"
 edt.Parameter = "tbParameter"
 edt.Visible = True
 edt.Width = 150
 edt.TooltipText = "Use this search area to specify the TYPE of search to run for emails in your folder."
 edt.AddItem("Clear Search")
 edt.AddItem("QuickSearch Selected Folder")
 edt.AddItem("Create Report for")
 edt.Text = "Clear Search"
 edt.DropDownWidth = 250
 End If
 End Sub
Private Sub timer_Tick(sender As Object, e As System.EventArgs) Handles timer.Tick
 Try
 Dim strEnteredText As String = (editor.Text).Trim()
 Dim strSelectedSearchText As String = (edt.Text).Trim()
 If (strEnteredText).Length > 0 Then
 If ("Clear Search").Equals(strSelectedSearchText) Then
 editor.Text = ""
 strSearchText = ""
 Else
 strSearchText = strEnteredText
 If Not (editor.Text).Equals(strSearchText) Then editor.Text = strSearchText
 End If
 Else
 editor.Text = strSearchText
 End If
 editor.Visible = True
 Catch ex As Exception
 End Try
End Sub
 Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
End Sub
End Class

References:

Advertisements

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