Earlier in part 1 of disable local resources I described the aim of disabling the use of local resources. Today I will show you how to implement this.
There are two modules we’re going to develop
- This Project
- Resource check Class module
The resource check class module is giving us the event handlers that can be used by the solution. First Create a new class module and call it ResourceCheck.
Add to the class module the following line:
Public WithEvents App As Application
Each task in my project can have a the following situations.
- No resources assigned to a task
- All resources are enterprise resources
- All resources are local
- There are incomplete resources (enterprise resources without an account assigned to them)
Then for the different situations we’re creating an enumeration and a variable storing the Checkresults:
Private Enum ResourceCheckResults   Â
    Unknown   Â
    AllEnterprise   Â
    LocalResource   Â
    IncompleteEnterpriseResource
End Enum
Dim ResCheckResult As ResourceCheckResults
Then to create a function which checks a resource collection and returns site what type of resources are found.
Public Function GetCheckResultForResources(Resources) As Integer
   Dim CheckResults As ResourceCheckResults: CheckResults = Unknown   Â
   For Each Resource In Resources       Â
        If Resource.Enterprise Then                  Â
              If CheckResults = Unknown Or CheckResults = AllEnterpriseResources Then               Â
                   If Resource.EMailAddress = “” Or Resource.WindowsUserAccount = “” Then                   Â
                        CheckResults = IncompleteEnterpriseResource               Â
                   Else                   Â
                        CheckResults = AllEnterpriseResources               Â
                   End If                          Â
             End If       Â
         Else          Â
               CheckResults = LocalResource       Â
         End If   Â
    Next Resource      Â
    GetCheckResultForResources = CheckResults
End Function  Â
Ok so the base work has now been done. Now we need to make sure that the results are checked when a user updates the resources in a project. There are quite a few event handlers relevant here.
The event handlers I’ve considered here are:
- App_ProjectBeforeAssignmentChange – Occurs before the user changes the value of an assignment field.
- App_ProjectBeforeAssignmentDelete – Occurs before an assignment is removed or replaced
- App_ProjectBeforeResourceChange – Occurs before the user changes the value of a resource field
- App_ProjectBeforeTaskChange – Occurs before the user changes the value of a task field
- App_ProjectBeforeSave2 – Occurs before a project is saved.
ProjectBeforeAssignmentChange
When a resource is assigned to a task the following event handler will check if an Enterprise or a local user is added. When a local user is added the assignement is cancelled.
Private Sub App_ProjectBeforeAssignmentChange(ByVal asg As Assignment, ByVal Field As PjAssignmentField, ByVal NewVal As Variant, Cancel As Boolean) Â Â Â
    On Error GoTo ErrHandler:   Â
    If (asg.Resource.Enterprise) Then       Â
          If (ResCheckResult = Unknown Or ResCheckResult = AllEnterprise) Then           Â
                ResCheckResult = AllEnterprise       Â
          End If       Â
      Else       Â
          ResCheckResult = LocalResource          Â
          MsgBox (asg.Resource.Name & ” is NOT an enterprise user”)       Â
          Cancel = True   Â
     End If      Â
      Exit Sub ErrHandler:   Â
           ‘ No need to do anything just ignore the error   Â
      Exit Sub   Â
End Sub
ProjectBeforeAssignmentDelete
As part of the solution I changed the colour of the resource. Depending on if we’re talking about a Local or an Enterprise resource. Therefore when a resource is deleted the colour of the task’s resource needs to be reviewed
Private Sub App_ProjectBeforeAssignmentDelete(ByVal asg As Assignment, Cancel As Boolean) Â Â Â
    On Error GoTo ErrHandler:   Â
    ResCheckResult = GetCheckResultForResources(asg.Task.Resources)   Â
    Select Case CheckResults        Â
           Case LocalResource             Â
                   asg.Application.ActiveCell.FontColor = pjRed        Â
            Case Unknown             Â
                  asg.Application.ActiveCell.FontColor = pjBlack        Â
           Case AllEnterprise             Â
                  asg.Application.ActiveCell.FontColor = pjBlack        Â
           Case IncompleteEnterpriseResource             Â
                  asg.Application.ActiveCell.FontColor = pjBlue   Â
            End Select   Â
          Exit Sub
         ErrHandler:   Â
              ‘ No need to do anything just ignore the error   Â
         Exit Sub
End Sub
ProjectBeforeResourceChange
Private Sub App_ProjectBeforeResourceChange(ByVal res As Resource, ByVal Field As PjField, ByVal NewVal As Variant, Cancel As Boolean) Â Â Â
    If (res.Enterprise) Then       Â
           Debug.Print (res.Name & ” is an enterprise user”)   Â
     Else       Â
            MsgBox (NewVal & ” is NOT an enterprise user”)       Â
           Cancel = True   Â
     End If
End Sub
ProjectBeforeTaskChange
Private Sub App_ProjectBeforeTaskChange(ByVal tsk As MSProject.Task, ByVal Field As PjField, ByVal NewVal As Variant, Cancel As Boolean) Â Â Â
     On Error GoTo ErrHandler:   Â
     CheckResults = GetCheckResultForResources(tsk.Resources)      Â
     Select Case CheckResults        Â
            Case LocalResource             Â
                     tsk.Application.ActiveCell.FontColor = pjRed        Â
             Case Unknown             Â
                      tsk.Application.ActiveCell.FontColor = pjBlack        Â
            Case AllEnterprise             Â
                     tsk.Application.ActiveCell.FontColor = pjBlack        Â
            Case IncompleteEnterpriseResource             Â
                      tsk.Application.ActiveCell.FontColor = pjBlue   Â
     End Select      Â
     Exit Sub   Â
      ErrHandler:   Â
            ‘ Just ignore the errors   Â
    Exit Sub       Â
End Sub
ProjectBeforeSave2
Private Sub App_ProjectBeforeSave2(ByVal pj As Project, ByVal SaveAsUi As Boolean, ByVal Info As EventInfo) Â Â Â
    Dim MyResource As Resource   Â
    Dim ResourceID   Â
    Dim LocalResources As String   Â
    Dim EnterWithoutEmail As String   Â
    Dim EnterWithoutAccount As String   Â
    Dim NewColour      Â
    If ThisProject.Application.Projects.Count > 0 Then          Â
        For Each Task In ThisProject.Application.Projects(1).Tasks           Â
               If Not Task Is Nothing Then               Â
                   For Each MyResource In Task.Resources                   Â
                       If Not MyResource.Enterprise Then
                            LocalResources = LocalResources + MyResource.Name + “;”
                       End If
                        If MyResource.EMailAddress = “” Then
                             EnterWithoutEmail = EnterWithoutEmail + MyResource.Name + “;”
                         End If
                         If MyResource.WindowsUserAccount= “” Then
                              EnterWithoutAccount = EnterWithoutAccount + MyResource.Name + “;”
                        End If
               Next MyResource
           End If
       Next Task
  End If
   If LocalResources + EnterWithoutEmail + EnterWithoutAccount <> “” Then
       If LocalResources = “” Then
           MsgBox (“Please report the following details to your system administrator: ” & vbNewLine & “Enterprise resources without email: ” & EnterWithoutEmail & vbNewLine & vbNewLine & “Enterprise resources without account: ” & EnterWithoutAccount)
       Else
           If EnterWithoutEmail + EnterWithoutAccount = “” Then
              MsgBox (“Please remove the following local resources: ” & vbNewLine & LocalResources)
            Else
              MsgBox (“Please remove the following local resources: ” & vbNewLine & LocalResources & vbNewLine & vbNewLine & “Please report the following details to your system administrator: ” & vbNewLine & “Enterprise resources without email: ” & EnterWithoutEmail & vbNewLine & vbNewLine & “Enterprise resources without account: ” & EnterWithoutAccount)
              Info.Cancel = True
           End If Â
      End If Â
  End If
End Sub
Then now the final steps. How do we get the Event handlers to run.
Open the This project and specify the following variables and Enumeration:
Dim MyApp As New ResourceChecks
Private Enum TaskResourceCheckResults   Â
    Unknown   Â
    AllEnterpriseResources   Â
    LocalResource   Â
    IncompleteEnterpriseResource
End Enum
Then there are a couple of Subs to make our live easier:
UpdateResourceColours
Private Sub UpdateResourceColours(Tasks As Object) Â Â Â Â Â Â
  Dim NewColour   Â
  Dim OldColour   Â
  Dim TaskLine      Â
  Â
  On Error GoTo ErrHandler:   Â
  TaskLine = 0   Â
   For Each Task In Tasks       Â
        TaskLine = TaskLine + 1       Â
        If Task Is Nothing Then           Â
               ‘Blank lines to be ignored       Â
        Else           Â
              SelectRow Task.UniqueID, RowRelative:=False
               SelectTaskField Row:=TaskLine, RowRelative:=False, Column:=”Resource Names”
               OldColour = MyApp.App.ActiveCell.FontColor
               If Task.Resources.Count = 0 Then
                       NewColour = pjBlack
               Else
                          CheckResults = MyApp.GetCheckResultForResources(Task.Resources)
                           Select Case CheckResults
                              Case LocalResource
                                   NewColour = pjRed
                              Case Unknown
                                    NewColour = pjBlack
                              Case AllEnterprise
                                   NewColour = pjBlack
                              Case IncompleteEnterpriseResource
                                   NewColour = pjBlue
                           End Select
             End If
      End If
      If NewColour <> OldColour Then
           Font Color:=NewColour
      End If
Next Task
    SelectRow Tasks(1).UniqueID, RowRelative:=False
Exit Sub
  ErrHandler:
     If MyApp.App Is Nothing Then
          MsgBox (“Please reload your project”)
           Exit Sub
       End If
        Exit Sub
 End Sub
Report_LocalResources
Private Sub Report_LocalResources(ByVal pj As Project)
On Error GoTo ErrHandler: Â Â Â Â Â Â
         For Each Resource In pj.Resources
                 If Not Resource.Enterprise Then
                       If Resource.Name = Empty Then               Â
                                 ‘ An Empty resource has been added to the project resources. This one needs to be removed too                                  If LocalResourceList = Empty Then
                                      LocalResourceList = Resource.ID
                                      NumLocalResources = 1
                               Else
                                     LocalResourceList = LocalResourceList & “,” & Resource.ID
                                    NumLocalResources = NumLocalResources + 1
                              End If
                   Else
                              If LocalResourceList = Empty Then
                                     LocalResourceList = Resource.Name
                                      NumLocalResources = 1
                              Else
                                      LocalResourceList = LocalResourceList & “,” & Resource.Name
                                    NumLocalResources = NumLocalResources + 1
                             End If
                  End If
             End If
     Next Resource
        If LocalResourceList <> Empty Then
             MsgBox (NumLocalResources & ” local resources have been found Please remove these resources from the Resources, Add Resources, Build team from Enterprise option in the Project”)
      End If
       Exit Sub
ErrHandler:    ‘ Ignore Errors   Â
        Exit Sub
End Sub
Ok so now all the hard work has been done all we need to do is create an object when a project is opened and call ourfunctions and subs to update the resource names column’s colour.
Private Sub Project_Open(ByVal pj As Project)
         On Error GoTo ErrHandler:
                   Dim CheckResult As TaskResourceCheckResults
                    CheckResults = Unknown
                    Set MyApp.App = Application
                 UpdateResourceColours pj.Tasks
                   Exit Sub
ErrHandler:
         ‘ Ignore Errors
        Exit Sub
End Sub
Private Sub Project_BeforeSave(ByVal pj As Project)
On Error GoTo ErrHandler:
      Dim LocalResourceList As String: LocalResourceList = Empty
     Dim NumLocalResources As Integer    Debug.Print (“BeforeSave”)
      UpdateResourceColours pj.Tasks
      Report_LocalResources pj
     Exit Sub   Â
ErrHandler:
     ‘ Ignore Errors
     Exit Sub
End Sub
