首页 > 解决方案 > MS Project - 禁用资源

问题描述

我正在创建一个 Microsoft Project 加载项。根据资源的自定义文本列的值(例如 Text30),我可以在尝试将资源分配给任务时禁用资源显示吗?

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

在此处输入图像描述

标签: c#vb.netvstoms-project

解决方案


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

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

如果资源的 Text30 值不为“Active”,程序将不允许用户将资源添加到任务。

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


推荐阅读