Add a Custom Task Pane to Project 2010 (Norm Estabrook)

  • Comments 7

Good news! Project 2010 supports custom task panes! Bad News! Getting one to appear in a Project 2010 is not so obvious. Good News! It’s a lot easier than I make it sound.

In this post, I’ll show you how to add a custom task pane to Project 2010 by using a Project 2010 or Project 2007 project template in Visual Studio.

The “skinny” on task panes in Microsoft Office Project

Project 2007 does not support custom task panes. Project 2010 does support custom task panes. The thing that might throw you off is that the Project 2010 project template in Visual Studio does not expose that cool CustomTaskPanes field. You know, that field that enables you to access a CustomTaskPaneCollection object by typing this.CustomTaskPanes or Me.CustomTaskPanes?

No worries. We can just create a CustomTaskPaneCollection object ourselves. The exact code that you use to accomplish that depends on the .NET Framework version that your project targets.

For now, add a User Control item to your project. This provides the design surface for your custom task pane. Next, identify which version of the .NET Framework you are targeting. If you created a brand new Project 2010 project, then odds favor that your project targets the .NET Framework 4.  However, it never hurts to check. Here is a helpful topic that shows you how to examine that little piece of info -  How to: Target a Specific .NET Framework Version or Profile.

After you do all of that, add this code:

For projects that target the .NET Framework 4:

[VB]

    Private myUserControl1 As MyUserControl
    Private myCustomTaskPane As Microsoft.Office.Tools.CustomTaskPane
    Private myCustomTaskPaneCollection As Microsoft.Office.Tools.CustomTaskPaneCollection

    Private Sub ThisAddIn_Startup() Handles Me.Startup
        myUserControl1 = New MyUserControl
        myCustomTaskPaneCollection = Globals.Factory.CreateCustomTaskPaneCollection _
        (Nothing, Nothing, "CustomTaskPanes", "CustomTaskPanes", Me)
        myCustomTaskPane = myCustomTaskPaneCollection.Add(myUserControl1, "My Task Pane")
        myCustomTaskPane.Visible = True
    End Sub

    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
        myCustomTaskPaneCollection.Dispose()
    End Sub

[C#]

    private MyUserControl myUserControl1;
    private Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane;
    private Microsoft.Office.Tools.CustomTaskPaneCollection myCustomTaskPaneCollection;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        myUserControl1 = new MyUserControl();
        myCustomTaskPaneCollection =
            Globals.Factory.CreateCustomTaskPaneCollection
            (null, null, "CustomTaskPanes", "CustomTaskPanes", this);

        myCustomTaskPane = myCustomTaskPaneCollection.Add(myUserControl1, "My Task Pane");
        myCustomTaskPane.Visible = true;

    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
        myCustomTaskPaneCollection.Dispose();
    }
For projects that target the .NET Framework 3.5:

[VB]

    Private myUserControl1 As MyUserControl
    Private myCustomTaskPane As Microsoft.Office.Tools.CustomTaskPane
    Private myCustomTaskPaneCollection As Microsoft.Office.Tools.CustomTaskPaneCollection

    Private Sub ThisAddIn_Startup() Handles Me.Startup
        myUserControl1 = New MyUserControl
        myCustomTaskPaneCollection = New Microsoft.Office.Tools.CustomTaskPaneCollection _
            (Me.ItemProvider, Me.HostContext, "MyTaskPane", Me, "MyTaskPane")
        myCustomTaskPane = myCustomTaskPaneCollection.Add(myUserControl1, "My Task Pane")
        myCustomTaskPane.Visible = True
    End Sub

    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
        myCustomTaskPaneCollection.Dispose()
    End Sub

[C#]

    private MyUserControl myUserControl1;
    private Microsoft.Office.Tools.CustomTaskPane myCustomTaskPane;
    private Microsoft.Office.Tools.CustomTaskPaneCollection myCustomTaskPaneCollection;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        myUserControl1 = new MyUserControl();
        myCustomTaskPaneCollection = new Microsoft.Office.Tools.CustomTaskPaneCollection
                (this.ItemProvider, this.HostContext, "MyTaskPane", this, "MyTaskPane");

        myCustomTaskPane = myCustomTaskPaneCollection.Add(myUserControl1, "My Task Pane");
        myCustomTaskPane.Visible = true;
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
        myCustomTaskPaneCollection.Dispose();
    }

Want to Use a Project 2007 project? No problem. Check this out.

So what if you are using Visual Studio 2008 and you do not have a Project 2010 project template?  No problem. Project 2010 can host a Project 2007 add-in. All you have to do is configure your project settings to open the Project 2010 executable on start up. The name of that executable file is WINPROJ.exe.

Right click your project node in Solution Explorer, click Properties, click the the Debug tab, and point away! Here is a screenshot of how I did it.

image

Norm E.

 

Leave a Comment
  • Please add 3 and 1 and type the answer here:
  • Post
  • Thanks, Norm. I have recently started development using VSTO and will keep an eye on this official blog.

  • Thanks Norm!

    Until now, Projects seemed to be the orhpan of the VSTO family. Now we have the same look and feel for all of our office addins.

  • Thanks for the great post Norm,

    Following your suggestion we've attached a custom task pane to Project 2010 in a VSTO addin for Peoject. It works fine until for some reason during runtime the following exception is thrown and the project application object is no longer accessible.

    1) System.Runtime.InteropServices.InvalidComObjectException was unhandled by user code

     Message=COM object that has been separated from its underlying RCW cannot be used.

     Source=mscorlib

    Any thougts on the matter are highly appreciated :-)

    thanks again,

    Roy

  • Hi Roy,

    Can you tell me a bit more about what the application is doing when this error appears?  I have not seen this issue arise as a result of adding custom task panes to Office applications per se.  However, I have seen numerous public threads that site the exact error message that you are receiving, but within other contexts.

    You said that the project application object is no longer accessible. Does this error appear when your code attempts to access a member by using Globals.ThisAddIn.Application from the task pane code file?

    Because this issue has not been specifically tied to custom task panes, any specific information that you can provide me would help me investigate your issue.

    Thanks!

    Norm E.

  • Thanks for the post. I am having an issue with an add-in for project 2010.  I can get the task pane to come up fine and everything is hunky dory.  But if the add in is disabled from project and I go to reenable it I get an

    {"Value cannot be null.\r\nParameter name: o"} Null Argument Exception.

    The Stack Trace is:

      at System.Runtime.InteropServices.Marshal.IsComObject(Object o)

      at System.Runtime.InteropServices.ComAwareEventInfo.AddEventHandler(Object target, Delegate handler)

      at Microsoft.Office.Tools.CustomTaskPaneImpl..ctor(_CustomTaskPane customTaskPane, UserControl control)

      at Microsoft.Office.Tools.CustomTaskPaneCollectionImpl.AddHelper(UserControl control, String title, Object window)

      at Microsoft.Office.Tools.CustomTaskPaneCollectionImpl.Add(UserControl control, String title, Object window)

      at Mentor.ThisAddIn.ThisAddIn_Startup(Object sender, EventArgs e) in C:\Users\tbell\Documents\Visual Studio 2010\Projects\Mentor\Mentor\ThisAddIn.cs:line 115

      at Microsoft.Office.Tools.AddInImpl.OnStartup()

      at Microsoft.Office.Tools.AddInImpl.AddInExtensionImpl.Microsoft.Office.Tools.EntryPoint.OnStartup()

      at Microsoft.Office.Tools.AddInBase.OnStartup()

      at Mentor.ThisAddIn.FinishInitialization() in C:\Users\tbell\Documents\Visual Studio 2010\Projects\Mentor\Mentor\ThisAddIn.Designer.cs:line 55

      at Microsoft.Office.Tools.AddInBase.Microsoft.Office.Tools.EntryPoint.FinishInitialization()

      at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.ExecutePhase(ExecutionPhases executionPhases)

      at Microsoft.VisualStudio.Tools.Office.Runtime.DomainCreator.ExecuteCustomization.Microsoft.VisualStudio.Tools.Office.Runtime.Interop.IExecuteCustomization2.ExecuteEntryPoints()\

    Any help would be appreciated.

  • Thanks...your article really helped.

  • Thanks Norm! Trying to find this for hours

Page 1 of 1 (7 items)

Add a Custom Task Pane to Project 2010 (Norm Estabrook)