ms-project如何将资源自定义字段复制到任务自定义字段

时间:2019-02-18 16:19:47

标签: ms-project

在MS Project(2013)中,谁能告诉我如何将资源自定义字段(从资源视图选项卡复制)到子项目任务的自定义字段(任务视图选项卡)?找到以下内容以将其从分配字段(资源使用情况视图)复制到任务字段,但是我不知道如何解决资源字段...

Sub CopyAssignmentFieldToTask()
Dim t As Task
Dim ts As Tasks
Dim a As Assignment
Set ts = ActiveProject.Tasks
For Each t In ts
If Not t Is Nothing Then
t.Text5 = ""
For Each a In t.Assignments
'change the following line to use
'for a different custom field
t.Text5 = t.Text5 & ", " & a.Text5
Next a
End If
Next t
End Sub

来源:http://zo-d.com/blog/archives/programming/working-with-task-and-assignment-fields-vba.html

编辑:非常感谢Rachel ...对于将来的引用,这是在子项目中循环的完整答案:

Sub CopyResourceUnitstoTasksv2()
Dim t As Task
Dim a As Assignment
Dim mProj As Project

Set mProj = ActiveProject
For Each Subproject In mProj.Subprojects
For Each t In ActiveProject.Tasks
    If Not t Is Nothing Then
        For Each a In t.Assignments
            t.Number2 = a.Resource.Number1
        Next a
    End If
Next t
Next Subproject
End Sub

1 个答案:

答案 0 :(得分:0)

Assignment对象具有一个Resource属性,该属性返回Resource对象:

Sub CopyResourceFieldToTask()
    Dim t As Task
    Dim a As Assignment
    Dim t5 As String
    For Each t In ActiveProject.Tasks
        If Not t Is Nothing Then
            t5 = vbNullString
            For Each a In t.Assignments
                t5 = t5 & ", " & a.Resource.Text5
            Next a
            If Len(t5) > 2 Then
                t.Text5 = Mid(t5, 3)
            Else
                t.Text5 = vbNullString
            End If
        End If
    Next t
End Sub