MS Project - 禁用资源

MS Project - Disable resource

我正在创建 Microsoft Project 加载项。根据资源的自定义文本列(比如 Text30)的值,我能否在尝试将资源分配给任务时禁止其显示?

因此,例如,如果某行的 Text30 值为 Inactive,则项目文件用户不应将其分配给任务,但它仍应位于资源 Sheet。下面,第 1 行将作为资源提供,而第 2 行在将资源分配给任务时不会显示。

在 Rachel 的 linked 问题的帮助下,我设法找到了解决方法。我添加了一个名为 Active Status 的文本列(在我的例子中是 Text30)。然后,使用 Application.ProjectBeforeTaskChange 我检查了新值是否是具有值 Active 的列 Text30 的资源。 Here 是 MS Project 事件的 link。下面是代码:

Imports Microsoft.Office.Interop.MSProject

Private Sub Application_ProjectBeforeTaskChange(tsk As Task, Field As PjField, NewVal As Object, ByRef Cancel As Boolean) Handles Application.ProjectBeforeTaskChange

    CheckResourceValidity(NewVal, Cancel)

End Sub


Private Sub CheckResourceValidity(NewVal As Object, ByRef Cancel As Boolean)

    Dim res As Resource
    Dim newValList = NewVal.Split(",")

    If Not IsStartup(newValList) Then

        For Each splitNewVal In newValList
            ' currentProject is the currently active Project
            res = currentProject.Resources.Item(splitNewVal)

            If splitNewVal = res.Name Then

                If res.Text30 <> "Active" Then
                    MessageBox.Show("You are trying to assign an inactive resource to a task. Please choose an active resource.",
                                    "Assigning inactive resource", MessageBoxButtons.OK, MessageBoxIcon.Error)

                    Cancel = True
                End If

            End If
        Next

    End If

End Sub


' The event ProjectBeforeTaskChange is called when a .mpp file with data inside is opened
Private Function IsStartup(newValList As Object) As Boolean

    Dim res As Resource

    For Each x In newValList

        Try
            res = GlobalVariables.currentProject.ProjResources.Item(x)
        Catch ex As System.Runtime.InteropServices.COMException
            Return True
        End Try

    Next

    Return False

End Function

如果资源没有 'Active' 的 Text30 值,程序将不允许用户将资源添加到任务。

注意: 仅当您通过甘特图分配资源时才会触发此事件。如果您通过 资源选项卡 -> 分配资源 进行分配,则不会触发该事件。事件 ProjectBeforeAssignmentNew 将通过 分配资源 和甘特图触发。唯一的缺点是我没有找到一种方法来访问通过 ProjectBeforeAssignmentNew 事件更改的值。您可以找到事件 here.