Export (0) Print
Expand All
Expand Minimize

Building Outlook 2007 Solutions by Using Visual Studio 2005 Tools for the Office System SE

Office 2007

Summary:   Learn how to create a Microsoft Office Outlook 2007 add-in by using Microsoft Visual Studio 2005 Tools for the Microsoft Office system. Expose the functionality of the add-in from the 2007 Microsoft Office Fluent Ribbon and from a custom task pane. (17 printed pages)

Each release of Microsoft Office Outlook brings new features to make your digital communications easier. Microsoft Office Outlook 2007 is no different. As a user, I’m excited. As a developer, I’m more excited. Outlook 2007 brings enormous improvements to its programming model. In particular, for managed developers working with Microsoft Visual Studio 2005 Tools for the Microsoft Office system, building rich solutions with Microsoft Visual Basic or Microsoft Visual C# has become easier. Visual Studio 2005 Tools for Microsoft Office enables you to create rich application-level add-ins for Outlook 2007 (and other Office hosts) that can take advantage of the Microsoft Office Fluent Ribbon, application-level task panes, and Outlook 2007 form regions.

In this article, you learn to build an application add-in that helps track your flight travel and mileage. To start, you create a custom form region to track additional data related to a flight, such as airline, flight times, time zones, and miles flown. You access the custom form region from a custom button that you add to the Office Fluent Ribbon. You add the custom form region and make Office Fluent Ribbon modifications to the built-in Calendar item. And finally, you build an application-level task pane to make it easy to see all of the trips defined.

NoteNote

While you can write Visual Basic and Visual C# in a similar fashion, the code written in this article generally uses built-in language constructs—such as the Visual Basic MsgBox and WithEvents features—instead of a Microsoft .NET Framework alternative.

Whenever possible, you should use Visual Studio 2005 Tools for Microsoft Office to build application add-ins for Microsoft Office applications. Visual Studio 2005 Tools for Microsoft Office helps resolve many of the issues inherent to creating managed add-ins, such as using application domain (AppDomain) isolation, registering your assembly instead of mscoree.dll, and making sure your add-in run when only signed add-ins are allowed. To create an Outlook 2007 add-in using Visual Studio 2005 Tools for Microsoft Office, do the following.

To create the add-in project

  1. Start Visual Studio 2005.

    NoteNote

    This article assumes that you installed Visual Studio using the default General Development settings. If you selected Visual Basic Developer, for example, the menu items are displayed slightly differently. You might need to adjust these instructions for your specific environment.

  2. On the File menu, click NewProject.

  3. In the New Project dialog box, in the list of project types, click Visual Basic to expand it. Select Office from the list of project types, and then click 2007 Add-ins.

  4. In the Templates pane, select Outlook Add-in.

  5. Name the add-in FlightTracker, specify a location for your add-in, and then click OK.

  6. On the Window menu, click Close All Documents.

Building a form region is a multi-step process. First, you must enable the developer features inside Outlook 2007. Second, use the Outlook form design tools to design the form region.

To create the form region

  1. Start Outlook 2007.

  2. On the Tools menu, click Options.

  3. In the Options dialog box, click the Other tab.

  4. In the General section, click Advanced Options.

  5. In the In all Microsoft Office programs section, select the Show Developer tab in Ribbon and Show add-in user interface errors options, and click OK twice to close the Options dialog box.

    The Show Developer tab in Ribbon option enables the commands that are necessary to build a form region. The Show add-in user interface errors option helps you find issues as you customize the Office Fluent Ribbon.

  6. On the Tools menu, point to Forms, and then click Design a Form.

  7. In the Design Form dialog box, select Appointment, and then click Open.

  8. Click the Developer tab on the Office Fluent Ribbon.

  9. In the Design group, click the Form Region arrow, and then click New Form Region.

    Outlook adds a new page with a tab labeled (Form Region) to the Form Designer, and displays the Field Chooser dialog box.

  10. Click the Control Toolbox icon in the Tools group on the Office Fluent Ribbon to display the Control Toolbox.

  11. Right-click anywhere in the Controls tab and select the Custom Controls option.

  12. In the Additional Controls dialog box, scroll through the list of controls, select Microsoft Office Outlook Label Control and Microsoft Office Outlook Text Box Control, and then click OK.

  13. Drag five labels and five text box controls onto the form.

  14. Right-click each label and click Properties to set the Caption property as follows:

    Airline

    Flight Number

    Departure Airport

    Arrival Airport

    Miles

  15. Right-click each text box control and click Properties to set the Name property as follows:

    txtAirline

    txtFlightNumber

    txtDeparture

    txtArrival

    txtMiles

  16. Right-click each text box control and click Properties.

  17. In the Properties dialog box, click the Value tab to create a user-defined field.

  18. Click New and fill in the New Field dialog box as shown in Table 1.

    Table 1. User-defined control properties

    Control

    Name

    Type

    Format

    txtAirline

    Airline

    Text

    Text

    txtFlightNumber

    FlightNumber

    Text

    Text

    txtDeparture

    Departure

    Text

    Text

    txtArrival

    Arrival

    Text

    Text

    txtMiles

    Miles

    Number

    Truncated

  19. On the Office Fluent Ribbon, in the Design group, click the Form Region arrow, and then click Save Form Region As.

  20. In the Save Form Region As dialog box, save the file as FlightData.ofs.

    Close the open appointment form. When Outlook prompts you to save the changes to the item underlying the designer, click No.

After you create the form region, you must add the OFS (Outlook Form Storage) file to your add-in project in Visual Studio 2005 as an embedded resource.

To add the OFS file to the add-in project

  1. In Solution Explorer, right-click the FlightTracker node, and then click Properties.

  2. In the Project designer, click the Resource tab.

  3. Click the down arrow next to Add Resource, and then click Add Existing File.

  4. In the Add existing file to resources dialog box, navigate to the FlightData.ofs file, and then click Open.

  5. On the File menu, click Save All.

  6. On the File menu, click Close to close the Property window.

Next, you create an XML manifest file that describes how Outlook 2007 should host the form region.

To create the manifest file

  1. On the Project menu, click Add New Item.

  2. In the Add New Item dialog box, select XML File.

  3. In the Name box, type FileDataManifest, and then click Add.

  4. In Solution Explorer, double-click FileDataManifest.xml to open the code window.

  5. Replace the contents of the file with the following XML markup.

    <?xml version="1.0" encoding="utf-8"?>
    <FormRegion xmlns=
    "http://schemas.microsoft.com/office/outlook/12/formregion.xsd">
       <name>FlightTrackerDetail</name>
       <title>Flight Information</title>
       <formRegionType>adjoining</formRegionType>
       <showInspectorCompose>true</showInspectorCompose>
       <showInspectorRead>true</showInspectorRead>
       <showReadingPane>true</showReadingPane>
       <hidden>false</hidden>
       <addin>FlightTrackerVB</addin>
       <version>1.0</version>
    </FormRegion>
    
    
  6. On the File menu, click Save All to save the file.

  7. On the Window menu, click Close All Documents.

  8. In Solution Explorer, right-click FileDataManifest.xml, and then click Exclude From Project.

  9. In Solution Explorer, right-click the FlightTracker node and select Properties.

  10. In the Project designer, click the Resource tab.

  11. Click the down arrow next to Add Resource, and then click Add Existing File.

  12. In the Add existing file to resources dialog box, navigate to the FlightDataManifest.xml file in the FlightTracker folder, and then click Open.

  13. On the File menu, click Save All, and then click Close to close the Project designer.

  14. On the Window menu, click Close All Documents.

After you add the form region and its manifest to your add-in, you must add the code to pass the form region to Outlook 2007 when you open an appointment item. Because you might have more than one Appointment inspector open at a time, and thus multiple form region instances, you need to create a collection manager class and an instance class.

To create the form region manager and instance classes

  1. In Visual Studio 2005, on the Project menu, click Add Class.

  2. In the Add New Item dialog box, type RegionManager in the Name box, and then click Add.

  3. On the Project menu, click Add Class.

  4. In the Add New Item dialog box, type FlightRegion, and then click Add.

  5. Replace the entire contents of the RegionManager class file with the following code.

    Imports System.Collections.Generic
    
    Public Class RegionManager
      Implements IDisposable
    
    Private Const FORM_REGION_NAME As String = "FlightTrackerDetail"
    
      Private regionInstances As Dictionary(Of Outlook.FormRegion, _
        FlightRegion)
      Private disposedValue As Boolean = False
    
      Public Sub New()
        Me.regionInstances = New Dictionary(Of Outlook.FormRegion, _
          FlightRegion)
      End Sub
    
      Protected Overrides Sub Finalize()
        Dispose(False)
        MyBase.Finalize()
      End Sub
    
      Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
          If disposing Then
          End If
        End If
        Me.disposedValue = True
      End Sub
    
      Public Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
      End Sub
    End Class
    
    

    This block of code provides support for tracking multiple instances of the FlightRegion class by using a generic Dictionary object. In addition, it provides support for object cleanup by implementing IDisposable.

    Next, you need to implement the Outlook._FormRegionStartup interface. This interface provides the entry points to enable Outlook 2007 to load your custom form region when you open an appointment item.

  6. Replace the entire contents of the FlightRegion class file with the following code.

    Public Class FlightRegion
      Implements IDisposable
    
      Private WithEvents regionInstance As Outlook.FormRegion
      Private disposedValue As Boolean = False
    
      Sub New(ByVal Region As Outlook.FormRegion)
        regionInstance = Region
      End Sub
    
      Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
          If disposing Then
          End If
        End If
        Me.disposedValue = True
      End Sub
    
      Public Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
      End Sub
    End Class
    
    
  7. Modify the RegionManager class to implement the Outlook._FormRegionStartup interface. The class declaration should look like this.

    Public Class RegionManager
      Implements IDisposable, Outlook._FormRegionStartup
    
    
  8. Visual Studio 2005 adds the interface members (a total of four methods). Modify the members as follows.

    Public Sub BeforeFormRegionShow( _
      ByVal FormRegion As Outlook.FormRegion) _
      Implements Outlook._FormRegionStartup.BeforeFormRegionShow
    
      Dim ri As New FlightRegion(FormRegion)
      Me.regionInstances.Add(FormRegion, ri)
    End Sub
    
    Public Function GetFormRegionIcon( _
      ByVal FormRegionName As String, ByVal LCID As Integer, _
      ByVal Icon As Outlook.OlFormRegionIcon) As Object _
      Implements Outlook._FormRegionStartup.GetFormRegionIcon
    
      Return Nothing
    End Function
    
    Public Function GetFormRegionManifest( _
      ByVal FormRegionName As String, ByVal LCID As Integer) _
      As Object _
      Implements Outlook._FormRegionStartup.GetFormRegionManifest
    
      If FormRegionName = FORM_REGION_NAME Then
        Return My.Resources.FileDataManifest
      Else
        Return Nothing
      End If
    End Function
    
    Public Function GetFormRegionStorage( _
      ByVal FormRegionName As String, ByVal Item As Object, _
      ByVal LCID As Integer, _
      ByVal FormRegionMode As Outlook.OlFormRegionMode, _
      ByVal FormRegionSize As Outlook.OlFormRegionSize) As Object _
      Implements Outlook._FormRegionStartup.GetFormRegionStorage
    
      If FormRegionName = FORM_REGION_NAME Then
        Return My.Resources.FlightData
      Else
        Return Nothing
      End If
    End Function
    
    
    

When queried by Outlook 2007, the methods that you implemented provide the manifest and binary region files that you created previously. In addition, you use the BeforeFormRegionShow method to track each new appointment instance you open. To properly clean up, you must tell the RegionManager class when a FlightData instance is closed. You must add a Close event to the FlightData instance and handle that event from the RegionManager class.

To add a Close event to the FlightData instance

  1. Modify the FlightRegion class to expose a Close event.

    Public Event Close(ByVal Region As Outlook.FormRegion)
    Add code to handle the Close event for the FlightRegion instance and raise the new custom event.
    Private Sub FlightRegion_Close( _
      ByVal Region As Microsoft.Office.Interop.Outlook.FormRegion) _
      Handles Me.Close
    
      If Not Me.disposedValue Then
        RaiseEvent Close(Region)
      End If
    
      Me.Dispose(True)
    End Sub
    
    
  2. Modify the overridden version of the Dispose method in the FlightRegion class to look like the following code.

    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
      If Not Me.disposedValue Then
        If disposing Then
          regionInstance = Nothing ' <-- Added line of code
        End If
      End If
      Me.disposedValue = True
    End Sub
    
    
  3. In the RegionManager class, add the following method to handle the Close event for a FlightData instance.

    Private Sub OnFlightDataClose(ByVal region As Outlook.FormRegion)
    
      Dim regionInstance As FlightRegion = Nothing
      Me.regionInstances.TryGetValue(region, regionInstance)
    
      If regionInstance IsNot Nothing Then
        RemoveHandler regionInstance.Close, _
          AddressOf OnFlightDataClose
        Me.regionInstances.Remove(region)
      End If
    End Sub
    
    
  4. Finally, modify the existing BeforeFormRegionShow method to use the Close event.

    Public Sub BeforeFormRegionShow( _
      ByVal FormRegion As Outlook.FormRegion) _
      Implements Outlook._FormRegionStartup.BeforeFormRegionShow
    
      Dim ri As New FlightRegion(FormRegion)
      ' Added line of code below.
      AddHandler ri.Close, AddressOf OnFlightDataClose
      Me.regionInstances.Add(FormRegion, ri)
    End Sub
    
    

    The last piece of code modifies the add-in class to load an instance of the RegionManager when Outlook is ready.

  5. Add the following class member to the ThisAddIn class in the ThisAddIn.vb node.

    Private mFormRegionManager As RegionManager
    
    
  6. Add the following code to override the RequestService method of the ThisAddIn class to load the RegionManager.

    Protected Overrides Function RequestService( _
      ByVal serviceGuid As System.Guid) As Object
    
      If serviceGuid = GetType(Outlook._FormRegionStartup).GUID Then
        If mFormRegionManager Is Nothing Then
          Me.mFormRegionManager = New RegionManager()
        End If
    
        Return Me.mFormRegionManager
      End If
    
      Return MyBase.RequestService(serviceGuid)
    End Function
    
    
  7. On the File menu, click Save All.

  8. On the Window menu, click Close All Documents.

You have the necessary code to make your custom form region available to Outlook. However, before you try to run it, you must add a key to the Windows registry.

To add the add-in key to the registry

  1. Click Start, click Run, type Notepad, and then click OK.

  2. Add the following text.

    Windows Registry Editor Version 5.00

    [HKEY_CURRENT_USER\Software\Microsoft\Office\Outlook\FormRegions\IPM.Appointment]

    "FlightTrackerDetail"="=FlightTrackerVB"

  3. On the File menu, click Save.

  4. In the File name dialog box, type FlightData.reg.

  5. Click the Save as type list, and then select All Files.

  6. Choose a convenient location, and click Save.

  7. In Windows Explorer, double-click the FlightData.reg file. Click Yes when prompted to add the changes, and then click OK.

    This adds the information from FlightData.reg to the registry.

In the next steps, you test the project by creating an appointment.

To test the project

  1. In Visual Studio 2005, on the Debug menu, click Start Debugging.

  2. In Outlook 2007, create a new appointment item.

    Note how your custom form region appears at the bottom of the appointment item’s inspector at the bottom of the first page.

  3. Fill in the data, including some flight information.

  4. Save the appointment and switch to Calendar view.

  5. Select the appointment you just created.

    If necessary, turn on the Reading Pane and note how the region is displayed.

  6. On the File menu, click Exit to close Outlook 2007 and return to Visual Studio 2005.

Now that you have a custom form region that lets you enter and save your flight data, create an application-level task pane to make it easy to find all of your appointments that have flight information. You define the user interface of a task pane by building a Windows Forms user control. Then you write code to make the task pane available to Outlook 2007.

To create the task pane

  1. In Visual Studio 2005, on the Project menu, click Add User Control.

  2. In the Add New Item dialog box, type FlightSearcher, and then click Add.

  3. Add the user controls listed in Table 2 to the design surface of the new user control.

    Table 2. User controls

    Control

    Name

    Label

    lblStartDate

    DateTimePicker

    dtpStartDate

    Label

    lblEndDate

    DateTimePicker

    dtpEndDate

    Button

    btnLookup

    ListBox

    lstFlights

  4. Set the user control properties listed in Table 3.

    Table 3. User control properties

    Control

    Property

    Value

    lblStartDate

    Text

    Start Date:

    lblEndDate

    Text

    End Date

    dtpStartDate

    Format

    Short

    dtpEndDate

    Format

    Short

    btnLookup

    Text

    Lookup

  5. On the View menu, click Code to open the code window.

  6. Replace the existing class with the following code.

    Public Class FlightSearcher
      Private hostApp As Outlook.Application = Nothing
    
      Public Sub New(ByVal host As Outlook.Application)
        Me.InitializeComponent()
        Me.hostApp = host
      End Sub
    
      Private Sub btnLookup_Click( _
        ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles btnLookup.Click
    
        If Me.dtpEndDate.Value < Me.dtpStartDate.Value Then
          MessageBox.Show( _
            "Your end date should be after your start date.", _
            "Change End Date", _
            MessageBoxButtons.OK, MessageBoxIcon.Warning)
          Exit Sub
        End If
    
        Me.lstFlights.Items.Clear()
        Dim sDate As String = Me.dtpStartDate.Text
        Dim eDate As String = Me.dtpEndDate.Text
    
        Dim filterTextBase As String = "@SQL=(" & _
          """http://schemas.microsoft.com/mapi/string/" & _
          "{00020329-0000-0000-C000-000000000046}/Airline""" & _
          "IS NOT NULL AND  "
        Dim filterDates As String = _
          """urn:schemas:calendar:dtstart"" >= '{0}' " & _
          "AND ""urn:schemas:calendar:dtend"" <= '{1}')"
    
        Dim filterText As String = filterTextBase & _
          String.Format(filterDates, sDate, eDate)
    
        Dim tbl As Outlook.Table = _
          hostApp.Session.GetDefaultFolder( _
            Outlook.OlDefaultFolders.olFolderCalendar).GetTable( _
            filterText, Outlook.OlTableContents.olUserItems)
    
        Const entryID As String = "EntryID"
        With tbl.Columns
          .RemoveAll()
          .Add("EntryID")
          .Add("Subject")
          .Add("Start")
          .Add("Airline")
        End With
        tbl.Sort("Start", True)
    
        Dim data As Outlook.Row
        Dim item As Outlook.AppointmentItem
        Dim aiw As AptItemWrapper
        Do Until tbl.EndOfTable
          data = tbl.GetNextRow()
    
          item = _
            CType(Me.hostApp.Session.GetItemFromID( _
            CStr(data(entryID))), Outlook.AppointmentItem)
    
          aiw = New AptItemWrapper(item)
          Me.lstFlights.Items.Add(aiw)
        Loop
      End Sub
    
      Private Sub lstFlights_DoubleClick( _
        ByVal sender As System.Object, ByVal e As System.EventArgs) _
        Handles lstFlights.DoubleClick
    
        Dim ai As AptItemWrapper = _
        CType(Me.lstFlights.SelectedItem, AptItemWrapper)
    
        ai.item.Display()
      End Sub
    
      Private Class AptItemWrapper
        Public item As Outlook.AppointmentItem
        Public Sub New(ByVal AptItem As Outlook.AppointmentItem)
          item = AptItem
        End Sub
        Public Overrides Function ToString() As String
          If item Is Nothing Then
            Return MyBase.ToString()
          Else
            Return item.Subject & vbTab & item.Start.ToString("d")
          End If
        End Function
      End Class
    End Class
    
    
  7. Double-click the ThisAddIn.vb node.

  8. In the ThisAddIn class, add the following code to the top of the source file.

    Imports Tools = Microsoft.Office.Tools
    Imports Core = Microsoft.Office.Core
    
    
  9. After the Public Class ThisAddIn statement, add the following code.

    Private fs As FlightSearcher
    Private ctp As Tools.CustomTaskPane
    
    
  10. Replace the ThisAddIn_Startup and ThisAddIn_Shutdown methods with the following code.

    Private Sub ThisAddIn_Startup( _
      ByVal sender As Object, ByVal e As System.EventArgs) _
      Handles Me.Startup
    
      fs = New FlightSearcher(Me.Application)
      ctp = Me.CustomTaskPanes.Add(fs, "Flight Searcher")
      ctp.DockPosition = _
      Core.MsoCTPDockPosition.msoCTPDockPositionBottom
      ctp.Visible = True
    End Sub
    
    Private Sub ThisAddIn_Shutdown( _
      ByVal sender As Object, ByVal e As System.EventArgs) _
      Handles Me.Shutdown
    
      Me.CustomTaskPanes.Remove(ctp)
      ctp = Nothing
    End Sub
    
    
  11. On the File menu, click Save All.

  12. On the Debug menu, click Start Debugging.

    In Outlook 2007, perform a search for one of the flight data appointments you created earlier. Double-click one of the results to view it. When you finish, close Outlook 2007 and return to Visual Studio 2005.

  13. On the Window menu, click Close All Documents.

One of the most innovative features in Outlook 2007 is the Office Fluent Ribbon. Whenever you open an item, such as a mail message or an appointment, you see the new user interface (UI). Visual Studio 2005 Tools for the 2007 Office system makes it easy to customize the ribbon with XML markup and a small amount of code. For this example, you add a new button to the Office Fluent Ribbon for the Appointment inspector. When you click the button, it opens a dynamically created Windows Form that hosts the user control you built. This enables you to search and see quickly if you defined a flight already when you are creating a new appointment.

To customize the Office Fluent Ribbon

  1. In Visual Studio 2005, on the Project menu, click Add New Item.

  2. In the Add New Item dialog box, select Ribbon support, type AptRibbon for the name, and then click Add.

  3. In Solution Explorer, double-click the AptRibbon.vb node to open the code window.

  4. Highlight and delete the entire commented block of text in the code window.

  5. In Solution Explorer, right-click the add-in project node, and then click Properties.

  6. In the Properties window, click the Resource tab.

  7. In Solution Explorer, drag the AptRibbon.xml node to the Resources surface.

  8. On the File menu, click Save All.

  9. On the File menu, click Close to close the Properties window.

  10. Double-click the AptRibbon.vb node to open the code window.

  11. In the AptRibbon class, replace the contents of the GetCustomUI method with the following code.

    Select Case ribbonID
      Case "Microsoft.Outlook.Post.Compose"
        Return My.Resources.AptRibbon
      Case Else
        Return String.Empty
    End Select
    
    
  12. In the AptRibbon class, delete all the text and code in the Helpers region.

  13. Double-click the ThisAddIn.vb node to open the ThisAddIn class and add the following class level variable.

    Private ribbonCode As AptRibbon
    
    
  14. In the ThisAddIn class, replace the existing RequestService method with this new version.

    Protected Overrides Function RequestService( _
      ByVal serviceGuid As System.Guid) As Object
    
      If serviceGuid = GetType(Outlook._FormRegionStartup).GUID Then
        If mFormRegionManager Is Nothing Then
          Me.mFormRegionManager = New RegionManager()
        End If
    
        Return Me.mFormRegionManager
      End If
    
      If serviceGuid = GetType(Office.IRibbonExtensibility).GUID Then
        If ribbonCode Is Nothing Then
          ribbonCode = New AptRibbon()
        End If
        Return ribbonCode
      End If
    
      Return MyBase.RequestService(serviceGuid)
    End Function
    
    
  15. To modify the XML markup for the Office Fluent Ribbon, replace the existing contents of AptRibbon.xml with the following markup.

    <customUI 
      xmlns="http://schemas.microsoft.com/office/2006/01/customui" 
      onLoad="OnLoad" >
      <ribbon>
        <tabs>
          <tab idMso="TabAppointment">
            <group id="GroupFlights"  label="Flights">
              <button id="FindFlights" 
                            size="large" 
                            label="Find Flights" 
                            screentip="Find Flights"
                            onAction="OnFindFlightsButtonClick" 
                            imageMso="FindDialog" />
            </group>
          </tab>
        </tabs>
      </ribbon>
    </customUI>
    
    
  16. In the AptRibbon class, remove the existing OnToggleButton1 method from the Ribbon Callbacks region.

  17. Add the following class level member to the AptRibbon class.

    Private hostApp As Outlook.Application
    
    
  18. Modify the current default constructor (the New subroutine).

    Public Sub New(ByVal host As Outlook.Application)
      hostApp = host
    End Sub
    
    
  19. In the Ribbon Callbacks region, add the following code.

    Public Sub OnFindFlightsButtonClick( _
      ByVal control As Office.IRibbonControl)
    
    
      Using frm As New Form
        Dim ctl As New FlightSearcher(hostApp)
        ctl.Dock = DockStyle.Fill
        frm.Text = "Find Flights"
        frm.Controls.Add(ctl)
        frm.ShowDialog(ForegroundWindow.Instance)
      End Using
    End Sub
    
    
  20. Under the Ribbon Callbacks region, add the following helper class inside the AptRibbon class. This class enables you to show a Windows Form as a modal dialog box from an add-in.

    Private Class ForegroundWindow
      Implements IWin32Window
    
      Private Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr
      Private Shared mwindow As New ForegroundWindow()
    
      Public Shared ReadOnly Property Instance() As IWin32Window
        Get
          Return mwindow
        End Get
      End Property
    
      Public ReadOnly Property Handle() As System.IntPtr Implements System.Windows.Forms.IWin32Window.Handle
    
        Get
          Return GetForegroundWindow()
        End Get
      End Property
    End Class
    
    
  21. Double-click the ThisAddIn.vb node to open the ThisAddIn class.

  22. Modify RequestService to pass the Outlook Application instance to the AptRibbon constructor.

    ribbonCode = New AptRibbon(Me.Application)
    
    
  23. On the File menu, click Save All.

  24. On the Debug menu, click Start Debugging.

  25. In Outlook 2007, open an existing appointment.

  26. Click the Find Flights button on the Ribbon. Search for flights. Close the dialog box and exit Outlook 2007.

  27. On the Window menu, click Close All Documents.

To remove the FlightTracker functionality from Outlook 2007, you can remove the appropriate keys from the registry.

To remove keys from the registry

  1. Click Start, click Run, type Regedit, and then click OK.

  2. Navigate to the following key.

    [HKEY_CURRENT_USER\Software\Microsoft\Office\Outlook]

  3. Expand the Addins subkey, right-click FlightTracker, and then click Delete. Click Yes to confirm the deletion.

  4. Expand the FormRegions subkey, right-click IPM.Appointments, and then click Delete. Click Yes to confirm the deletion, and then close Registry Editor.

Add-ins enable you to extend the functionality of your 2007 Microsoft Office system applications. As you saw in this article, you can easily provide access to this functionality from the Office Fluent Ribbon and custom task panes. Use the techniques outlined in this article to add a professional touch to your own applications.

Brian A. Randell is a senior consultant with MCW Technologies, LLC. Brian spends his time teaching Microsoft .NET-based technologies to developers, working with new and emerging technologies, and helping clients worldwide with system design and implementation. Brian is a Microsoft MVP. You can reach him from his blog.

Show:
© 2015 Microsoft