Creating links with lags in VBA RRS feed

  • Question

  • Hello, I need to write a macro that creates a link between two tasks (a finish-to-start is fine) with a lag of a certain number of days. I know there's a method called LinkTaskEdit but I can't figure out the syntax.... (I am not a programmer).

    Could anybody give me an example of the code?


    Friday, September 13, 2013 4:20 PM

All replies

  • Hi,

    There are several methods to do this but the simplest is the following. Suppose TSUC is the successortask and TPRD the predecessor task then the following may do it:

    Prtext=cstr(TPRD.ID) & "FS+14d"


    If you have a variable instead of the 14 you need another cstr function.


    Friday, September 13, 2013 6:26 PM
  • If you don't know the syntax, try recording a macro. I got:

    LinkTasksEdit From:=1, To:=2, Type:=1, Lag:="2d"

    Type of 1 is pjFinishToStart so I would use:

    LinkTasksEdit From:=1, To:=2, Type:=pjFinishToStart, Lag:="2d"

    Rod Gill

    The one and only Project VBA Book

    Rod Gill Project Management

    Saturday, September 14, 2013 11:28 AM
  • Thanks Rod, but if I run the macro twice (because I have to create two groups of tasks with the same link) the macro only works for the first set of tasks: for the second set it creates the tasks but then it sets the links on the fist set of tasks! It's like if it was using an absolute row reference).

    This is what I am doing:

        SelectTaskField Row:=0, Column:="Name"
        SetTaskField Field:="Name", Value:="a"
        SelectTaskField Row:=1, Column:="Name"
        SetTaskField Field:="Name", Value:="b"
        SelectTaskField Row:=-1, Column:="Name"
        SelectTaskField Row:=0, Column:="Name", Height:=1, Extend:=True
        LinkTasksEdit From:=1, To:=2, Type:=pjFinishToStart, Lag:="2d"

    Could you help me with this? Thanks a million!!

    Monday, September 16, 2013 3:43 PM
  • Vasco,

    Recording a macro can be very useful for finding out the syntax but unfortunately all parameters in a recorded macro are in fact absolute. The macro in your example only applies to the currently selected row of the current view. If you have another set of tasks you wish to link, you will need to modify the code to select the rows for those tasks.

    I think Jan's approach might be better since it operates directly on the task object and not on the object in the current view. Let's say you want to link task IDs 1 & 2 and 9 and 10 with a finish-to-start. For the first set (i.e. task 1 & 2) you want a lag of 14 days and for the second set (i.e. task 9 & 10) you want a lag of 5 days. The syntax to do that would be:

    ActiveProject.Tasks(1).Successors = ActiveProject.Tasks(2).ID & "FS+14d".


    ActiveProject.Tasks(9).Successors = Activeproject.Tasks(10).ID & "FS+5d)

    Hope this helps.


    Monday, September 16, 2013 4:28 PM