简体   繁体   中英

How to set assignment programmatically MS Project(Interop) using VB.NET

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.

在此处输入图像描述

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.

In order to create periodic assignments (eg day, week, month) you will need to use one of the TimeScaleData Methods (assignment, task, resource). https://docs.microsoft.com/en-us/office/vba/f1/timescaledata-method-project-vbapj-chm131262?f1url=%3FappId%3DDev11IDEF1%26l%3Den-US%26k%3Dk(vbapj.chm131262)%3Bk(TargetFrameworkMoniker-Office.Version%3Dv16)%26rd%3Dtrue

There is plenty of reference information about how to use Project's object model. I'm not sure how it might apply when using VB.net, but a good reference for Project VBA is available via the object browser. 在此处输入图像描述 John

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM