How to programmatically assign Lag in MSProject C#

187 Views Asked by At

Microsoft Docs LinkPredecessors Method is described as:

public void LinkPredecessors (object Tasks, Microsoft.Office.Interop.MSProject.PjTaskLinkType Link = Microsoft.Office.Interop.MSProject.PjTaskLinkType.pjFinishToStart, object Lag);

How can I assign a Lag value to "object Lag"? The below code works to assign predecessor and task link type, however, I cannot figure out how to add the lag.

Microsoft.Office.Interop.MSProject.PjTaskLinkType LinkType;

    var p = IApp.ActiveProject;
    foreach (var y in tasksPred)
    {
        int intTaskType = Convert.ToInt32(y.RelationshipType);

        switch (intTaskType)
        {
            case 0:
                LinkType = Microsoft.Office.Interop.MSProject.PjTaskLinkType.pjFinishToFinish;
                break;
            case 1:
                LinkType = Microsoft.Office.Interop.MSProject.PjTaskLinkType.pjFinishToStart;
                break;
            case 2:
                LinkType = Microsoft.Office.Interop.MSProject.PjTaskLinkType.pjStartToFinish;
                break;
            case 3:
                LinkType = Microsoft.Office.Interop.MSProject.PjTaskLinkType.pjStartToStart;
                break;
            default:
                LinkType = Microsoft.Office.Interop.MSProject.PjTaskLinkType.pjFinishToStart;
                break;
        }

        if (y.UniqueIDPredecessor != "")
        {
           p.Tasks[Convert.ToInt32(y.UniqueID)].LinkPredecessors(p.Tasks[Convert.ToInt32(y.UniqueIDPredecessor)], LinkType);
        }
    }
1

There are 1 best solutions below

0
On

The LinkPredecessors method is expecting a string for the lag. From the docs:

A string that specifies the duration of lag time between linked tasks.

Examples of this would be:

  • "2d" for a 2-working day lag
  • "30ed" for a 30-elapsed-day lag
  • "4h" for a 4-working-hour lag
  • "3w" for a 3-working-week lag
  • "30m" for a 30-working-minute lag

Adding the "e" before the time unit makes it elapased, as in calendar days. Otherwise, lag time follow the working calendar.