Search code examples
vbams-project

Using VBA to return a field value of a given task ID in MS Project


This is probably really simple (I am new to VBA in MS Project - Excel is more my specialty) but I am just looking to return a value of selected field in MS Project based on a given Task ID. I (unsuccessfully) tried this:

ActiveProject.Tasks.UniqueID(1).GetField(Number2)

...but hopefully that shows what I am trying to achieve which is finding the value of the Number2 field in the first Task of a project.


Solution

  • To get the value of the Number2 field of a particular Task by ID:

    ActiveProject.Tasks(3).Number2
    

    To get the value by Task Unique ID:

    ActiveProject.Tasks.UniqueID(3).Number2
    

    To get the value of the Number2 field of the first selected task:

    ActiveSelection.Tasks(1).Number2
    

    The GetField method is useful if a variable field name is to be used:

    ActiveProject.Tasks(3).GetField (pjTaskNumber2)