21 | 08 | 2017

Set Task Fore and Back Color

Since MS Project 2007 Microsoft has added the function to change color of the tasks. How to do this using vb.net is displayed below.

        Dim rgbcolor As Long
        'SET THE CELLCOLOR OF THE ACTIVE TASK TO RED
        pjApplication.SelectTaskField(Row:=t.ID, Column:="Name", RowRelative:=False) 'change t.ID for any row number and "Name" for a specific column 
        rgbcolor = pjApplication.ActiveCell.CellColor
        pjApplication.Font32Ex(CellColor:=Color.Red) 'set the color of the cell called NAME to red

        'SET THE FONTCOLOR OF THE TASK 2 TO BLUE
        pjApplication.SelectTaskField(Row:=1, Column:="Duration", RowRelative:=False) 'Change 1 for any row number and "Duration" for a sepcific column 
        rgbcolor = pjApplication.ActiveCell.FontColor
        pjApplication.Font32Ex(Color:=Color.Blue) 'set the forecolor of the cell called NAME to blue

 

Copy the code below to test it in a complete application. The code will create a sample project, add data and color the cell.

Imports Microsoft.Office.Interop.MSProject

    Sub CreateProjectAndSetCellColor()

        '****************************************
        '*** Code from VisibleVisual.com ********
        '****************************************

        'Create a sample mpp file
        Dim pjApplication As New Application
        Dim tsks As Tasks
        Dim t As Task

        pjApplication.FileNew(Template:="")
        pjApplication.Visible = True

        tsks = pjApplication.ActiveProject.Tasks

        'CREATE SAMPLE DATA
        t = tsks.Add("TASK 1")
        t.OutlineLevel = 1 'set the task level
        t = tsks.Add("SUBTASK 1")
        t.OutlineLevel = 2 'set the task level
        t = tsks.Add("SUBTASK 2")
        t.OutlineLevel = 2 'set the task level

        pjApplication.ViewApplyEx(Name:="&Gantt Chart", ApplyTo:=0)

        'THE CODE BELOW SETS COLORS
        'important make sure all task are visible.
        pjApplication.OutlineShowAllTasks()

        Dim rgbcolor As Long
        'SET THE CELLCOLOR OF THE ACTIVE TASK TO RED
        pjApplication.SelectTaskField(Row:=t.ID, Column:="Name", RowRelative:=False) 'change t.ID for any row number and "Name" for a sepcific column 
        rgbcolor = pjApplication.ActiveCell.CellColor
        pjApplication.Font32Ex(CellColor:=Color.Red) 'set the color of the cell called NAME to red

        'SET THE FONTCOLOR OF THE TASK 2 TO BLUE
        pjApplication.SelectTaskField(Row:=1, Column:="Duration", RowRelative:=False) 'Change 1 for any row number and "Duration" for a sepcific column 
        rgbcolor = pjApplication.ActiveCell.FontColor
        pjApplication.Font32Ex(Color:=Color.Blue) 'set the color of the cell called NAME to red

    End Sub

The result of the code above looks like this:

Login

Sign up now and upload your code to the website.

Help us to continue.....
Statistics
Articles View Hits
2330597
Latest Articles