vbams-project

How to select a Task on a Timeline on MSProject?


I am looking for a way to create a Timeline on MSProject, with colors matching key words in the "Text2" column of my tasks. For this, I select the tasks with the right key word, and use the "TaskOnTimeline" command. However, this command doesn't select the task it just inserted in the timeline, and I can't find in vba forums a command that seems to do so. Wouls anyone have an idea of a way to avoid this problem, or a specific command? (I use MSProject 2016 by the way)

Edit: Here is the code I use, I created two arrays before, L() (list of key words to colour the tasks on the Timeline) and Couleur() (the colours)

WindowActivate TopPane:=True
SelectRow Row:=1, RowRelative:=False

For i = 1 To ActiveProject.Tasks.Count

    SelectRow Row:=i, RowRelative:=False
    
    If Not ActiveProject.Tasks(i) Is Nothing Then
        If InStr(1, ActiveProject.Tasks(i).Text2, "Titre", 1) <> 0 Then
            WindowActivate TopPane:=False
            InsertTimelineBar
            WindowActivate TopPane:=True
            TaskOnTimeline
            'Rajouter de mettre la tache en legende
            
        Else
            If Not ActiveProject.Tasks(i).Text2 = "" Then
                For y = 0 To n
                    If InStr(L, ActiveProject.Tasks(i).Text2, L(y), 1) <> 0 Then
                        WindowActivate TopPane:=True
                        SelectRow Row:=i, RowRelative:=False
                        TaskOnTimeline
                        WindowActivate TopPane:=False
                        Font32Ex CellColor:=Couleur(y)
                        
                    End If
                Next y
            End If
        End If
    End If
Next i

The issue is when I add the task on the timeline; I try to colour the task added (on the timeline and not on the Gantt table), but the task is not selected, so the Font32Ex command doesn't work.


Solution

  • This is another example of how the Project object model is lacking--there is not a direct way to select a task on the timeline.

    However, you can use SelectCellRight (or SelectCellLeft) to step through the timeline tasks to find the one you want. Try using this routine to format the task after it is added to the timeline.

    Sub FormatTimelineTask(uid As Long, color As Long)
    
        WindowActivate TopPane:=False
    
        SelectAll
        
        Dim numTlTasks As Long
        numTlTasks = ActiveSelection.Tasks.Count
        SelectCellUp
        
        Dim l As Long
        For l = 1 To numTlTasks
            SelectCellRight
            If ActiveCell.Task.UniqueID = uid Then
                Font32Ex color:=color
                Exit For
            End If
        Next l
        
    End Sub
    

    Note: I only have Project 2013 available at the moment so I cannot test on 2016 which allows multiple rows on the Timeline.