Customizing the Project 2010 ribbon with a VSTO add-in
-
23 เมษายน 2553 18:21ผู้ดูแล
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.
- There are no changes necessary in the ThisAddIn.cs (or ThisAddIn.vb) file.
- 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).
- In the ManualTaskcolor.cs [Design] view, drag a Tab from the Toolbox\Office Ribbon Controls to the ribbon.
- Drag a Group to the new tab.
- Drag a Button (or a ToggleButton) to the group. Change the labels, button image, etc. as you wish.
- To match the VBA example in the SDK, you can set the OfficeImageID property of the button to DiagramTargetInsertClassic.
- 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.MSProjectPublic Class ManualTaskColor
Private Const WHITE As Integer = &HFFFFFF
Private Const LIGHT_BLUE As Integer = &HF0D9C6Dim app As MSProject.Application
Dim project As MSProject.ProjectPrivate Sub ManualTaskColor_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) _
Handles MyBase.Load
app = Globals.ThisAddIn.Application
End SubPrivate Sub tBtnManualTaskColor_Click(ByVal sender As System.Object, _
ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) _
Handles tBtnManualTaskColor.Click
ToggleManualTasksColor()
End SubSub ToggleManualTasksColor()
project = app.ActiveProject
Dim column As String = "Name"
Dim rowRelative As Boolean = False
Dim rgbColor As IntegerFor 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.CellColorExIf (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
- แก้ไขโดย Jim CorbinModerator 29 มิถุนายน 2553 19:46 Updated article in SDK
ตอบทั้งหมด
-
28 เมษายน 2553 23:17Are 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 เมษายน 2553 14:56Jim, you say "Use the .NET Framework 3.5". Is there any particular reason you recommend v3.5 over .NET 4?
-
4 พฤษภาคม 2553 14:02ผู้ดูแล
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.
-
4 พฤษภาคม 2553 15:39ผู้ดูแล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 พฤษภาคม 2553 21:55ผู้ดูแล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 มิถุนายน 2553 0:40ผู้ดูแล
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 กรกฎาคม 2553 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 -
8 กันยายน 2553 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:
- 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.
- Debug the addin or publish/install - doesn't matter which - and open Project 2007 with the addin loaded
- Create an Excel Visual Report
- Close the Visual Report dialogue box
- Close Project
- Wham!
Doesn't happen on Project 2010.
/Lars Hammarberg
-
13 กุมภาพันธ์ 2556 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