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

时间:2015-06-04 16:15:33

标签: vba ms-office microsoft-project-vba

基本上如标题所示,我试图遍历一个自定义字段,名为" Text3"与"任务所有者"相关联,一次下拉过滤一个资源。我是新来的,所以请耐心等待。

这是我的代码:

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
*************************************************************************

我遇到的问题是它没有为该特定资源设置过滤器而只是将其留空。如果我将标准作为例如约翰史密斯",它可以工作,但我试图使用变量名作为字符串来遍历所有资源。

任何帮助将不胜感激。 谢谢。

1 个答案:

答案 0 :(得分:0)

根据您的评论,此代码将构建一个存储在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