2
votes

I am currently trying to write a VBA code that will look at a project plan and delete all of the tasks that have zero work effort, but are not a milestone, from my plan. We have added a custom field called Key Milestone to capture whether a task is a milestone or not. The reason we are not using the existing Milestone field is that not all of the tasks with zero work effort and zero duration are necessarily milestones.

I was previously unfamiliar with the GetField function in VBA and I have been working through a couple of tries at incorporating this to 'read' the custom field as a part of this code. Here is what I have so far:

Sub DeleteMsProjectTask()
Dim proj As Project
Dim t As Task
Set proj = ActiveProject
For Each t In proj.Tasks
If t.OutlineLevel > 1 And t.Work = 0 Then
If GetField(FieldNametoFieldConstant("Key Milestone") = Yes Then
Else
t.Delete
End If
End If
Next t
End Sub

This is not working as it doesn't read the milestone field correctly. Thanks in advance for the help!

1

1 Answers

0
votes

GetField is a method of the Task object, so you need to preface it with your task object variable, e.g. t.GetField. Also, you missed a closing parentheses. And since GetField returns a string, you need to compare to a string--in other words, the word Yes needed to be in quotes. Since the criteria for deletion was actually a "No" value, I simplified your code accordingly.

Sub DeleteMsProjectTask()
Dim proj As Project
Dim t As Task
Set proj = ActiveProject
For Each t In proj.Tasks
    If t.OutlineLevel > 1 And t.Work = 0 Then
        If t.GetField(FieldNameToFieldConstant("Key Milestone")) = "No" Then
            t.Delete
        End If
    End If
Next t
End Sub