How do I automatically create Summary and Subtasks in Microsoft Project using VBA

喜欢而已 提交于 2019-12-23 15:56:36

问题


So I have a Timeline in Microsoft Excel which contains a variety of information. I created an array of my own custom type which read in this data but now I need to output my array onto Project.

My array consists of a custom datatype as following:

Type customtype

  1. Summary Name (The subtask would go under their respective summary task)
  2. Number of subtasks
  3. Another array called TaskList()

This array called TaskList() would then hold as many subtasks inside as the number of tasks. All subtasks in TaskList for that respective array index belong to the same Summary name and would be in this format:

Type taskList

  1. Subtask Name
  2. startDate
  3. endDate

How would I go about now given that I have all the necessary info (name/summary name/start & end dates) and be able to in VBA automatically loop through and put this into MS Project?

Many thanks,

My custom structs:

Type TimelineInfo
    taskName As String
    dateStart As Date
    dateEnd As Date
End Type

Type resourceSummary
    beginningDate As Date
    endingDate As Date
    resourceName As String
    numberOfTasks As Integer
    taskList() As TimelineInfo
End Type

The resourceSummary would be the Summary Task, with all tasks in taskList() going underneath that respective resource summary name.

I've declared these two variables,

Dim timelineArray() As TimelineInfo
Dim groupsArray() As resourceSummary

groupsArray is indexed from 0 to 7, meaning that there are 8 summary tasks needed to be made with as many subtasks inside them. The array is already filled with data now I just need to output into project.

z = 1

For i = LBound(groupsArray) To UBound(groupsArray)
    For j = 0 To (groupsArray(i).number - 1)
        tempName = groupsArray(i).taskList(j).taskName
        pj.Tasks.Add Name:=tempName, before:=z
        pj.Tasks(z).Start = groupsArray(i).taskList(j).date
        pj.Tasks(z).Finish = groupsArray(i).taskList(j).dateEnd
        z = z + 1
    Next j
Next i

My Problem Above however is that I still need to create summary tasks to put all those subtasks inside. The summary task will be from the resourceSummary custom datatype and will use

beginningDate As Date
endingDate As Date
resourceName As String

回答1:


I'm not sure about your problem is but I'll work from this:

   Proj.Find "UniqueID", "equals", pj.Tasks(z-7).ID
   SelectRow pj.Tasks(z-7).ID, False, z
   OutlineIndent



回答2:


If you are working from Excel, likely your data is in a worksheet. If so, moving it to arrays of custom types is adding unnecessary complexity. That said, if you insist on using types, know that three elements of your Summary task type are irrelevant: beginningDate and endDate are calcluated fields for summary tasks, and you should remove numberOfTasks as you already have that information as the size of the taskList array.

Here is the code to add tasks to a new project from your arrays of custom types:

Sub AddTasks()

Dim i As Integer
Dim j As Integer
Dim z As Integer

For i = LBound(groupsArray) To UBound(groupsArray)
    z = z + 1
    pj.Tasks.Add groupsArray(i).resourceName
    pj.Tasks(z).OutlineLevel = 1
    For j = LBound(groupsArray(i).taskList) To UBound(groupsArray(i).taskList)
        z = z + 1
        pj.Tasks.Add groupsArray(i).taskList(j).taskName
        pj.Tasks(z).Start = groupsArray(i).taskList(j).dateStart
        pj.Tasks(z).Finish = groupsArray(i).taskList(j).dateEnd
        pj.Tasks(z).OutlineLevel = 2
    Next j
Next i

End Sub

This presumes that pj is declared as a MSProject.Project and that it contains no tasks.

Here is the code that I used to mock up sample data:

Dim groupsArray(4) As resourceSummary
Dim timelineArray(2) As TimelineInfo

Sub MockUpData()

Dim i As Integer
For i = 0 To 2
    timelineArray(i).taskName = "Sub task " & i
    timelineArray(i).dateStart = Date + i * 7
    timelineArray(i).dateEnd = (Date + i * 7) + 4
Next i
For i = 0 To 4
    groupsArray(i).resourceName = "Summary " & i
    groupsArray(i).taskList = timelineArray
Next i

End Sub


来源:https://stackoverflow.com/questions/51136478/how-do-i-automatically-create-summary-and-subtasks-in-microsoft-project-using-vb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!