使用 VBA Microsoft project 2010 一次迭代一个字段过滤器的宏

Macro to iterate through a field filter one at a time using VBA Microsoft project 2010

基本上正如标题所暗示的那样,我正在尝试遍历一个名为 "Text3" 的自定义字段,该字段与 "Task Owner" 关联,一次下拉过滤一个资源。我是新手,所以请多多包涵。

这是我的代码:


Sub Macro1()
' Macro Macro1
' Macro Recorded Fri 5/29/15 by Valencia, Jonathan.
' suppose to go down the task owner filtered list for each resource and
' print to xps

    Dim res As resource, name As String

    'apply the gantt view first
    ViewApply name:="gantt chart"
    'expand all tasks
    OutlineShowAllTasks
    'apply the late task filter
    FilterApply name:="Late Tasks"

For Each res In ActiveProject.Resources
    name = res.name


    'apply filter to task owner as the resource
    'checks to see if filter is set on the application first
    If Not ActiveProject.AutoFilter Then
        Application.AutoFilter
    End If

    Application.SetAutoFilter FieldName:="Text3", _
                FilterType:=pjAutoFilterCustom, _
                Test1:="contains", Criteria1:=name


    'export to xps with the resources' name
    DocumentExport FileName:=name, FileType:=pjXPS



Next res


End Sub
*************************************************************************

我遇到的问题是它没有为特定资源设置过滤器,只是将其留空。如果我给出标准,例如 "john smith",它会起作用,但我正在尝试使用变量名称作为字符串来遍历所有资源。

如有任何帮助,我们将不胜感激。 谢谢。

根据您的评论,此代码将构建一个存储在 Text3 字段中的唯一名称列表,然后遍历它们以创建 XPS 报告。

Sub CreateXpsReports()

    ' build unique list of names store in Text3
    On Error Resume Next
    Dim Names As New Collection
    Dim tsk As Task
    For Each tsk In ActiveProject.Tasks
        Names.Add tsk.Text3, tsk.Text3
    Next tsk

    On Error GoTo 0

    'apply the gantt view first
    ViewApply name:="gantt chart"
    'expand all tasks
    OutlineShowAllTasks
    'apply the late task filter
    FilterApply name:="Late Tasks"

    Dim name As Variant
    For Each name In Names
        'This was the only line I was missing for the code to work.
        On Error Resume Next           

        'apply filter to task owner as the resource
        'checks to see if filter is set on the application first
        If Not ActiveProject.AutoFilter Then
            Application.AutoFilter
        End If

        Application.SetAutoFilter FieldName:="Text3", _
                    FilterType:=pjAutoFilterCustom, _
                    Test1:="contains", Criteria1:=name

        'export to xps with the resources' name
        DocumentExport FileName:=name, FileType:=pjXPS

    Next name

End Sub