MS Project 2007 VBA用于检索任务和自定义字段

时间:2011-08-18 10:40:43

标签: vba custom-fields ms-project

我有一个简单的MS Project文件(2007),只有几个任务。 我还创建了一个名为VBATest的自定义字段,并根据两个项目任务为此自定义字段指定了值。

我想检索一个项目任务列表和分配给自定义字段的值,如下所示;

ProjectTask | VBATest <--Custom field
------------|--------
Task1       | vba1
Task2       | vba2

我是从Access 2007 VBA执行此操作的,因为这是最终代码的最终结果。 我可以使大部分工作,但我似乎无法从Assignments对象中读取自定义字段值。你有什么想法? 感谢

这是我到目前为止所做的。

Sub LoadProjectFile()

Dim pjApp As MSProject.Application
Dim FileToOpen
Dim Proj As MSProject.Project
Dim Project_Task As Task
Dim fd As FileDialog

Set pjApp = New MSProject.Application

If pjApp Is Nothing Then
MsgBox "Project is not installed"
End
End If

pjApp.Visible = True
AppActivate "Microsoft Project"

Set fd = Application.FileDialog(msoFileDialogFilePicker)
fd.Filters.Clear
fd.Filters.Add "Microsoft Project Files", "*.mpp"
fd.AllowMultiSelect = False
fd.Show
If (fd.SelectedItems.Count = 0) Then
    'Application.GetOpenFilename("Microsoft Project Files (*.mpp), *.mpp")
    pjApp.Quit
    Set pjApp = Nothing
    Exit Sub
End If

pjApp.FileOpen fd.SelectedItems(1)
Debug.Print "Project_Task_Name~CustomField"

Dim ass As Assignment
For Each Project_Task In pjApp.ActiveProject.Tasks
            If Not Project_Task Is Nothing Then

                For Each ass In Project_Task.Assignments
                    assignCFVal = assignCFVal & "," & ass.VBATestField '<<PROBLEM Line
                Next ass

               Debug.Print Project_Task.Name & "~" & assignCFVal
               assignCFVal = ""
            End If
        Next Project_Task

pjApp.FileClose pjDoNotSave
pjApp.Quit
Set pjApp = Nothing

End Sub

1 个答案:

答案 0 :(得分:0)

事实证明,我不需要为此使用Assignments对象。 SetField方法将返回我需要的内容,如下所示;

For Each Project_Task In pjApp.ActiveProject.Tasks
            If Not Project_Task Is Nothing Then
              assignCFVal = Project_Task.SetField(FieldNameToFieldConstant("VBATestField"))
              Debug.Print Project_Task.Name & "~" & assignCFVal
            End If
Next Project_Task