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: 

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.

Next, Install the Windows SDK.

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

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

        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
        elementHost.Dock = DockStyle.Fill
    End Sub

Comments (3)

  1. My depth of knowledge isn’t deep when it comes to VSTO (Visual Studio Tools for Office), however this

  2. Fala pessoal, Agora vou tentar começar a postar dicas de WPF e Silverlight com uma freqüência maior….