Home > Software engineering >  How to set assignment programmatically MS Project(Interop) using VB.NET
How to set assignment programmatically MS Project(Interop) using VB.NET

Time:09-06

I am trying to create an MPP file (MS Project) using VB.NET in a windows forms application using the MS Project Interop. I have been able to, create the project, load the resources and the tasks, but now I am stuck on how to assign the values for a resource to a task. I need to set it at the month level.

enter image description here

Since the values can be different depending on the month, I cant figure out how to even begin.

The months and years could be different. I need to be able to get the month/year and value and assign it to that task and resource.

Any ideas, because I do not know where to start. Since you can zoom in and out in project to view it by day, week, month, qtr, year, it is confusing how to just set it by month. Not sure if TimeScale has anything to do with it.

Any help would be very much appreciated. There isnt much out there in terms of coding for MS Project like there is for excel or other office products.

CodePudding user response:

In order to create periodic assignments (e.g. day, week, month) you will need to use one of the TimeScaleData Methods (assignment, task, resource). enter image description here John

CodePudding user response:

To set assignment work by month, use the TimeScaleData method using the pjTimescaleMonths TimeScaleUnit parameter.

Here's a vba example of a helper routine that sets work for an assignment for a month:

Sub SetWorkForMonth(asn As Assignment, dteStart As Date, hours As Long)

    Dim dteEnd As Date
    dteEnd = DateAdd("m", 1, DateSerial(Year(dteStart), Month(dteStart), 1))
    
    Dim tsvs As TimeScaleValues

    Set tsvs = asn.TimeScaleData(StartDate:=dteStart, _
                                 EndDate:=dteEnd, _
                                 Type:=PjAssignmentTimescaledData.pjAssignmentTimescaledWork, _
                                 TimeScaleUnit:=PjTimescaleUnit.pjTimescaleMonths)
    tsvs(1).Value = hours * 60
    
End Sub

And here's sample code to demonstrate how it could be used:

Sub SampleAddAssignmentAndCustomTimeScale()

    Dim tsk As Task
    Set tsk = ActiveProject.Tasks.Add("LX21A...")
    tsk.Start = #8/10/2022#
    tsk.Duration = "120d"
    
    Dim asn As Assignment
    Set asn = tsk.Assignments.Add(, ActiveProject.Resources("Admin Functional Support 3").ID)
    SetWorkForMonth asn, #8/1/2022#, 10
    SetWorkForMonth asn, #9/1/2022#, 15
    SetWorkForMonth asn, #10/1/2022#, 15
    SetWorkForMonth asn, #11/1/2022#, 15
    SetWorkForMonth asn, #12/1/2022#, 10
    SetWorkForMonth asn, #1/1/2023#, 15
    SetWorkForMonth asn, #2/1/2023#, 20
    
End Sub

See also the TimeScaleValues object which is a collection of TimeScaleValue objects.

The example code will easily translate to vb.net

  • Related