21 | 08 | 2017

Set Row and Change Color

Since MS Project 2007 Microsoft has added the function to change color of the tasks. It is also possible to select a whole row and change its appearance. How to do this using vb.net is displayed below.

        Dim rgbcolor As Long
        'SET THE CELLCOLOR OF THE ACTIVE TASK TO RED
        pjApplication.SelectRow(Row:=t.ID, RowRelative:=False) 'change t.ID for any row number
        rgbcolor = pjApplication.ActiveCell.CellColor
        pjApplication.Font32Ex(CellColor:=Color.Red) 'set the color of the row red

        'SET THE FONTCOLOR OF THE TASK 1 TO BLUE
        pjApplication.SelectRow(Row:=1, RowRelative:=False) 'Change 1 for any row number 
        rgbcolor = pjApplication.ActiveCell.FontColor
        pjApplication.Font32Ex(Color:=Color.Blue) 'set the font color of row 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
        t = tsks.Add("SUBTASK 3")
        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.SelectRow(Row:=t.ID, RowRelative:=False) 'change t.ID for any row number
        rgbcolor = pjApplication.ActiveCell.CellColor
        pjApplication.Font32Ex(CellColor:=Color.Red) 'set the color of the row red

        'SET THE FONTCOLOR OF THE TASK 1 TO BLUE
        pjApplication.SelectRow(Row:=1, RowRelative:=False) 'Change 1 for any row number 
        rgbcolor = pjApplication.ActiveCell.FontColor
        pjApplication.Font32Ex(Color:=Color.Blue) 'set the font color of row to blue

    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
2330628
Latest Articles