用于检索任务和自定义字段的 MS Project 2007 VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7106081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MS Project 2007 VBA to retrieve tasks and custom fields
提问by Sivakanesh
I have a simple MS Project file (2007) with just couple of tasks.
I have also created a custom field called VBATest
and assigned values to this custom field against the two project tasks.
我有一个简单的 MS Project 文件(2007),只有几个任务。我还VBATest
针对两个项目任务创建了一个自定义字段,并为该自定义字段分配了值。
I would like to retrieve a list of project tasks and the value assigned to the custom field like this;
我想检索项目任务列表和分配给自定义字段的值,如下所示;
ProjectTask | VBATest <--Custom field
------------|--------
Task1 | vba1
Task2 | vba2
I'm doing this from Access 2007 VBA as this is where the final code will end up. I can get most of it working, but I can't seem to read the custom field value from the Assignments object. Do you have Any ideas? Thanks
我正在从 Access 2007 VBA 执行此操作,因为这是最终代码的结束位置。我可以让它大部分工作,但我似乎无法从 Assignments 对象读取自定义字段值。你有什么想法?谢谢
Here is what I have done so far.
这是我到目前为止所做的。
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
采纳答案by Sivakanesh
It turns out I don't need to use the Assignments object for this. The SetField method would return what I need as below;
事实证明,我不需要为此使用 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