sticky
Customizing the Project 2010 ribbon with a VSTO add-in RRS feed

  • General discussion

  • 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

    • Edited by Jim Corbin Tuesday, June 29, 2010 7:46 PM Updated article in SDK
    Friday, April 23, 2010 6:21 PM

All replies

  • 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?
    Wednesday, April 28, 2010 11:17 PM
  • Jim, you say "Use the .NET Framework 3.5". Is there any particular reason you recommend v3.5 over .NET 4?
    Thursday, April 29, 2010 2:56 PM
  • 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.

    Tuesday, May 4, 2010 2:02 PM
  • 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.
    Tuesday, May 4, 2010 3:39 PM
  • 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.
    Tuesday, May 18, 2010 9:55 PM
  • 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
    Wednesday, June 16, 2010 12:40 AM
    Moderator
  • 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
    Wednesday, July 21, 2010 12:50 PM
  • 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

     

    Wednesday, September 8, 2010 12:27 PM
  • 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

    Wednesday, February 13, 2013 4:56 PM
  • I want to do the same thing on Project Server 2010 (PWA) and call VBA macros.  Any suggestions?
    Friday, May 31, 2013 12:22 PM
  • Hi can you help me, look only want  put colo to my bars!!! but how!!

    Imports Microsoft.Office.Interop

    button

    Dim pjApplication As New MSProject.Application pjApplication.FileNew(Template:="") pjApplication.Visible = True pjApplication.OptionsCalendar(StartWeekOnMonday:=True, HoursPerDay:=24, HoursPerWeek:=168, DaysPerMonth:=30, StartTime:="7:00 a.m.", FinishTime:="7:00 a.m.") pjApplication.ScreenUpdating = True 'diasble visible screenupdates until the code is completed Dim tsks As MSProject.Tasks Dim t As MSProject.Task tsks = pjApplication.ActiveProject.Tasks t = tsks.Add("Programa: " + sacadescripcion.ExecuteScalar.ToString) t.OutlineLevel = 1 t.Start = '01/01/2014' t.Finish = '31/01/2014'

    'For Each t In tsks 'GanttBarFormat(tss:=Task.ID, MiddleColor:=pjGreen, StartColor:=pjGreen, EndColor:=pjGreen) 'Next

    and the for do not nothing, mark with red, not compile

    only color to the bar!

    im using vs2010 on VB forms, and project 2010 desktop version

    tnks a lot!


    Wednesday, February 19, 2014 12:27 AM