Customizing the Project 2010 ribbon with a VSTO add-in

مثبتة Customizing the Project 2010 ribbon with a VSTO add-in

  • 23 April 2010 18:21
    Moderator
     
     

    RE: the Project Professional 2010 forum thread: Project 2010: adding ribbon, which discusses the SDK article How to: Add a Custom Command to the Ribbon, and the use of SetCustomUI in VBA.

    NOTE: The code in this post is explained in the Project 2010 SDK article, How to: Use Managed Code to Add a Custom Command to the Ribbon.

    It is instructive to do the same exercise with VSTO. There are many advantages to using a VSTO add-in, including the ability to easily publish the solution with ClickOnce. As the thread discussion notes, there is no good way to distribute the VBA solution in the Global.MPT to other users.

    Basically, use Visual Studio 2010 and create a new Project 2010 Add-in project. Use the .NET Framework 3.5. You can use C# or VB. 

    1. There are no changes necessary in the ThisAddIn.cs (or ThisAddIn.vb) file.
    2. Right-click the project in Solution Explorer, and add a new item -- add a Ribbon (Visual Designer) item. In the code below, it is named ManualTaskColor.cs (or ManualTaskColor.vb).
    3. In the ManualTaskcolor.cs [Design] view, drag a Tab from the Toolbox\Office Ribbon Controls to the ribbon.
    4. Drag a Group to the new tab.
    5. Drag a Button (or a ToggleButton) to the group. Change the labels, button image, etc. as you wish.
    6. To match the VBA example in the SDK, you can set the OfficeImageID property of the button to DiagramTargetInsertClassic.
    7. Select the new button in the ribbon Design view, click the Events icon in the Properties pane, and then double-click the Click event to create the button_Click event handler.

    Here is the C# code in the ManualTaskColor.cs file. The code is ported from the VBA code in the SDK article:

    using System;
    using Microsoft.Office.Tools.Ribbon;
    using MSProject = Microsoft.Office.Interop.MSProject;

    namespace RibbonAddIn
    {
        public partial class ManualTaskColor
        {
            private const int WHITE = 0xFFFFFF;
            private const int LIGHT_BLUE = 0xF0D9C6;

            MSProject.Application app;
            MSProject.Project project;

            private void ManualTaskColor_Load(object sender, RibbonUIEventArgs e)
            {
                app = Globals.ThisAddIn.Application;
            }

            private void tBtnManualTaskColor_Click(object sender, RibbonControlEventArgs e)
            {
                ToggleManualTasksColor();
            }

            private void ToggleManualTasksColor()
            {
                project = app.ActiveProject;
                string column = "Name";
                bool rowRelative = false;
                int rgbColor;
               
                foreach (MSProject.Task t in project.Tasks)
                {
                    if ((t != null) && !(bool)t.Summary)
                    {
                        app.SelectTaskField(t.ID, column, rowRelative);
                        rgbColor = app.ActiveCell.CellColorEx;

                        if ((bool)t.Manual)
                        {
                            // Check whether the manual task color is white.
                            if (rgbColor == WHITE)
                            {
                                app.Font32Ex(CellColor:LIGHT_BLUE); // Change the background to light blue.
                            }
                            else
                            {
                                app.Font32Ex(CellColor:WHITE); // Change the background to white.
                            }
                        }
                        else
                        {
                            // The task is automatically scheduled, so change the background to white.
                            app.Font32Ex(CellColor:WHITE);
                        }
                    }
                }
            }
        }
    }

    _________________Just for kicks, here is the VB code in the ManualTaskColor.vb file, in you do the project in VB. The code is ported from the C# example above:

    Imports Microsoft.Office.Tools.Ribbon
    Imports MSProject = Microsoft.Office.Interop.MSProject

    Public Class ManualTaskColor

        Private Const WHITE As Integer = &HFFFFFF
        Private Const LIGHT_BLUE As Integer = &HF0D9C6

        Dim app As MSProject.Application
        Dim project As MSProject.Project

        Private Sub ManualTaskColor_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) _
                                         Handles MyBase.Load
            app = Globals.ThisAddIn.Application
        End Sub

        Private Sub tBtnManualTaskColor_Click(ByVal sender As System.Object, _
                                              ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) _
                                              Handles tBtnManualTaskColor.Click
            ToggleManualTasksColor()
        End Sub

        Sub ToggleManualTasksColor()
            project = app.ActiveProject
            Dim column As String = "Name"
            Dim rowRelative As Boolean = False
            Dim rgbColor As Integer

            For Each t As MSProject.Task In project.Tasks
                If (Not t Is Nothing) And (Not t.Summary) Then
                    app.SelectTaskField(t.ID, column, rowRelative)
                    rgbColor = app.ActiveCell.CellColorEx

                    If (t.Manual) Then
                        ' Check whether the manual task color is white.
                        If (rgbColor = WHITE) Then
                            app.Font32Ex(CellColor:=LIGHT_BLUE) ' Change the background to light blue.
                        Else
                            app.Font32Ex(CellColor:=WHITE) ' Change the background to white.
                        End If
                    Else
                        ' The task is automatically scheduled, so change the background to white.
                        app.Font32Ex(CellColor:=WHITE)
                    End If
                End If
            Next
        End Sub
    End Class

    __________________

    Have fun,

    --Jim

Semua Balasan

  • 28 April 2010 23:17
     
     
    Are there any examples where the ribbon is implemented in Project using ribbon xml with callbacks (VSTO and C#)?  I am getting errors doing this in Project using the same callback signatures that work in other Office apps.  If not, can the method shown here work with Visual Studio 2008?
  • 29 April 2010 14:56
     
     
    Jim, you say "Use the .NET Framework 3.5". Is there any particular reason you recommend v3.5 over .NET 4?
  • 04 Mei 2010 14:02
    Moderator
     
     

    SharePoint, Project Server, and Project client were developed with .NET 3.5. Using .NET 4.0 in some cases doesn't work with Project Server applications, such as workflow and configuring WCF. It's probably taking a chance if you mix 3.5 and 4.0 components, but I haven't done a lot of testing with 4.0.

  • 04 Mei 2010 15:39
    Moderator
     
     
    Visual Studio 2010 is required because it includes the templates for Project 2010 and correctly creates event handlers. The SDK update later in May includes an article that creates an event handler. I don't yet have an example using ribbon XML with callbacks.
  • 18 Mei 2010 21:55
    Moderator
     
     
    The May update of the Project 2010 SDK includes the How to: Use Managed Code to Add a Custom Command to the Ribbon article, and the SDK download includes the complete code.
  • 16 Juni 2010 0:40
    Moderator
     
     

    To all --

    I made a MSDN presentation on this to use the VSTO in VS2010 and VB.NET.  I create a ribbon and some executable code.

    If you wish a copy of the code (it is free) you contact me by visiting my blog http://www.msprojectblog.com

    When I dig out the actual Microsoft link, I will edit this post.

    Jim


    jeaksel at yahoo dot com
  • 21 Juli 2010 12:50
     
     

    Jim,

    I am looking for resources on doing the opposite. I want to remove/hide things on the ribbon.  One example is on the Project Center, under new I want to remove/hide the From SharePoint list option.

    Also, does anyone know how to find the ID of the existing ribbon controls.


    Jay Smith
  • 08 September 2010 12:27
     
     

    Project 2007 VSTO issue: I'm not sure if you've tried to create a VSTO add-in targeting Project 2007 - but I'd be glad if you could verify the following:

    1. Create a VSTO addin for Project 2007 using VS2008 or VS2010 targeting 3.5 - the addin doesn't have to do anything at all - just using the code in th newly created "empty" template will suffice.
    2. Debug the addin or publish/install - doesn't matter which - and open Project 2007 with the addin loaded
    3. Create an Excel Visual Report 
    4. Close the Visual Report dialogue box
    5. Close Project
    6. Wham!

    Doesn't happen on Project 2010.

    /Lars Hammarberg

    www.camako.se

     

  • 13 Februari 2013 16:56
     
     

    Looking at the SDK pre-requisites here: http://www.microsoft.com/en-gb/download/details.aspx?id=15511 it appears that XP is not supported.

    I am seeing crashes with a simple ribbon addin (similar to that above) for Project 2010 standard when tested on XP machines and am being told this is because XP is not supported.

    Can you categorically tell me if writing a VSTO addin for Project 2010, to customise a Ribbon is supported on Win 7 AND XP-SP3 (with .NET 3.5)?

    Many thanks