Using WPF controls inside a VSTO ActionsPane or CustomTaskPane

Mario Szpuszta showed a cool demo yesterday with WPF controls inside a VSTO 2005 ActionsPane for a Excel document.  I was inspired by his presentation to go figure out how to display a WPF control in a CustomTaskPane for a VSTO 2005 SE Add-in project.

First, you have to have a bunch of stuff installed.  Start with an installation of Visual Studio Professional or higher and Office 2007.

Next, Install VSTO 2005 SE for Office 2007:

https://www.microsoft.com/downloads/details.aspx?familyid=5E86CAB3-6FD6-4955-B979-E1676DB6B3CB&displaylang=en 

Next, install Microsoft .NET FX 3.0--note that this is already part of Vista so you only need to download this if you aren't running Vista yet. 

https://www.microsoft.com/downloads/details.aspx?familyid=10CC340B-F857-4A14-83F5-25634C3BF043&displaylang=en

Next, Install the Windows SDK.

https://download.microsoft.com/download/2/5/5/255a13fe-c09b-4618-8473-4de26c33bd97/Setup.exe

Finally, install Visual Studio 2005 extensions for .NET Framework 3.0.  This will give you WPF control projects.

https://www.microsoft.com/downloads/details.aspx?familyid=F54F5537-CC86-4BF5-AE44-F5A1E805680D&displaylang=en

Now, launch Visual Studio and Create a VSTO 2005 SE add-in project for the Office 2007 application of your choice (File->New Project..).  Look under the "Office" node for "Office 2003" or "Office 2007" and pick an add-in project for any application except Visio (Visio doesn't support Custom Task Panes).

Next, add a WPF Control Library project to your solution (File->Add New Project..)  Look under the "NET Framework 3.0" node and pick the "Custom Control Library (WPF)" project.  This project will have a UserControl1 project item.  Use the WPF designer or hack the XAML to design UserControl1.

Now that you have a cool WPF control designed, go back to your add-in project and add references to PresentationCore, PresentationFramework, WindowsBase, and WindowsFormsIntegration.

You also need to add a reference in your add-in project to your WPF control library project.

Next, add the following code to your "Startup" handler in your ThisAddin project item.  The basic idea here is you need an ElementHost control to host the WPF control inside of Windows Forms.  You also need a UserControl because VSTO 2005 SE task panes have to be a UserControl.  So we create an ElementHost control, set the "Child" property to a new instance of the WPF control, then we add the ElementHost control to a newly created Windows Forms UserControl and finally add that to the CustomTaskPanes collection.

    Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup

        Dim userControl As New System.Windows.Forms.UserControl
        Dim elementHost As New Integration.ElementHost
        elementHost.Child = New CustomControlLibrary1.UserControl1

        userControl.Controls.Add(elementHost)
        elementHost.Dock = DockStyle.Fill

        CustomTaskPanes.Add(userControl, "WPFControl Sample").Visible = True

    End Sub

You follow all the same instructions if you want to do this in an ActionsPane for Word 2003 or Excel 2003 document or template projects.

The code is slightly different however because the ActionsPane already is a user control.  Put this code in the Startup event of the ThisWorkbook project item in Excel or ThisDocument project item in Word:

    Private Sub ThisWorkbook_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
        Dim elementHost As New Integration.ElementHost
        elementHost.Child = New CustomControlLibrary1.UserControl1
        ActionsPane.Controls.Add(elementHost)
        elementHost.Dock = DockStyle.Fill
    End Sub