RE: the Project Professional 2010 forum thread: Project 2010: adding ribbon, which discusses the SDK articleHow to: Add a Custom Command to the Ribbon, and the use ofSetCustomUI 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 aRibbon (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 toDiagramTargetInsertClassic.
- 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