blog.atwork.at

news and know-how about microsoft, technology, cloud and more.

Project: Benutzerdefinierte Felder in andere Entität kopieren / Copy Custom Fields to Other Entity

  1. Deutsch
  2. English
  3. Code
  4. Code ECF

Deutsch

Häufig wird in Foren die Frage gestellt, weshalb Werte benutzerdefinierter Felder in einer Ansicht nicht zur Verfügung stehen. Das liegt üblicherweise daran, dass das Feld für eine Entität erstellt wurde, die in der gewünschten Ansicht nicht zur Verfügung steht. So kann es sich um ein Vorgangsfeld handeln, das in der Ansicht Ressource: Einsatz nicht angezeigt werden kann oder um ein Ressourcenfeld, das in der Ansicht Vorgang: Einsatz nicht verfügbar ist. Das ist auch dann der Fall, wenn die Berechnung für Zuordnungszeilen auf Abwärts zuordnen, wenn nicht manuell eingegeben aktiviert ist:

SNAGHTMLb01ce46

Die Feldwerte können jedoch per Makro für jede Zuordnung von einer Entität in die andere übertragen werden. Um die Werte lokaler Felder zu übertragen, können Sie den Code für lokale Felder einsetzen. Wenn Werte für Benutzerdefinierte Enterprise-Felder übertragen werden sollen, verwenden Sie den Code für diese Felder.
Nach Lauf des Makros stellt sich das Ganze dann so dar:

SNAGHTMLb04d481

In Makros implementieren / Implement Macros wird beschrieben, wie ein Makro in Project übernommen werden kann. Mehr Beispielmakros sind unter VBA zu finden.

English

There is often a question in forums why values of custom fields are not available in a specific view. This is usually because the field was created for an entity that is not available in the view you want. It can be an task field that can not be displayed in the Resource Usage view or a resource field that is not available in the Task Usage view. This is also the case if Calculation for assignment rows is set to Rolldown unless manually entered:

image

The field values can be transferred via macro for an assignment from one entity to another. To transfer the values of local fields, you can use the code for local fields. If values for Custom Enterprise fields are to be transmitted, use the code for these fields.

After running the macro, the project looks like this:

SNAGHTMLb1157d0

Implementing Macros / Implement Macros describes how to apply a macro to Project. More sample code is available at VBA.

Code

Sub CopyAssignmentFieldValueTaskToResource()
'***********************************************************************************
'Code is provided "AS IS" without warranty of any kind, either expressed or implied,
'including but not limited to the implied warranties of merchantability and/or
'fitness for a particular purpose.
'***********************************************************************************

'Copy assignment value from Task field "Text1" to Resource field "Text1"
'Prerequisite: For both fields, "Roll down unless manually specified" has to be enabled
'in Project - Custom Fields

Dim P As Project
Dim T As Task
Dim R As Resource
Dim At As Assignment
Dim Ar As Assignment

'P is active Project
Set P = ActiveProject

'Loop through all tasks
For Each T In P.Tasks
    'Ignore invalid tasks (empty rows)
    If Not T Is Nothing Then
        'Ignore summary tasks - no assignments should be there
        If Not T.Summary Then
            'Loop through all assignments
            For Each At In T.Assignments
                'Set R to resource of current assignment
                Set R = P.Resources(At.ResourceID)
                'Set ResourceAssignment to current TaskAssignment
                
                '****** INFORMATION ******************
                'Unfortunatley there is an issue when UniqueIDs for assignments did
                'not match between TaskAssignment and ResourceAssignment. If there is
                'a fix in future enable "otion 1" instead of "option 2"
                
                'Start option 1 ****************************************************
'                Set Ar = R.Assignments.UniqueID(At.UniqueID)
                'End option 1 ****************************************************
                
                
                'Start option 2 ****************************************************
                For Each Ar In R.Assignments
                    If Ar.TaskID = T.ID Then
                        'now we have the correct resource assignment
                        Exit For
                    End If
                Next Ar
                'End option 2 ****************************************************
                
                'Copy value
                Ar.Text1 = At.Text1
            Next At
        End If
    End If
Next T

End Sub

Sub CopyAssignmentFieldValueResourceToTask()
'***********************************************************************************
'Code is provided "AS IS" without warranty of any kind, either expressed or implied,
'including but not limited to the implied warranties of merchantability and/or
'fitness for a particular purpose.
'***********************************************************************************

'Copy assignment value from Resource field "Text1" to Task field "Text1"
'Prerequisite: For both fields, "Roll down unless manually specified" has to be enabled
'in Project - Custom Fields

Dim T As Task
Dim R As Resource
Dim At As Assignment
Dim Ar As Assignment

'P is active Project
Set P = ActiveProject

'Loop through all resources
For Each R In P.Resources
    'Ignore invalid resources
    If Not R Is Nothing Then
        'Loop through all assignments
        For Each Ar In R.Assignments
            'Consider only assignments for current project (there may by other projects open with the same resources)
            If Ar.Project = P Then
                'Set task to current assignment task
                Set T = P.Tasks(Ar.TaskID)
                'Set TaskAssignment to current ResourceAssignment
                
                '****** INFORMATION ******************
                'Unfortunatley there is an issue when UniqueIDs for assignments did
                'not match between TaskAssignment and ResourceAssignment. If there is
                'a fix in future enable "otion 1" instead of "option 2"
                
                'Start option 1 ****************************************************
'                Set At = T.Assignments.UniqueID(Ar.UniqueID)
                'End option 1 ****************************************************
                
                
                'Start option 2 ****************************************************
                For Each At In T.Assignments
                    If At.ResourceID = R.ID Then
                        'now we have the correct task assignment
                        Exit For
                    End If
                Next At
                'End option 2 ****************************************************

                'Copy value
                At.Text1 = Ar.Text1
            End If
        Next Ar
    End If
Next R

End Sub

Code ECF

Sub CopyAssignmentFieldValueTaskToResource_ECF()
    'Copy assignment value from Resource field "MyResourceField" to Task field "MyTaskField" (in this sample)
    'Prerequisite:
    '   - For both fields, "Roll down unless manually specified" has to be enabled
    '   - Resource and Task field names must not contain any spaces (" ")
    
    
    Dim P As Project
    Dim T As Task
    Dim R As Resource
    Dim At As Assignment
    Dim Ar As Assignment
    
    
    'P is active Project
    Set P = ActiveProject
    
    'Loop through all tasks
    For Each T In P.Tasks
        'Ignore invalid tasks (empty rows)
        If Not T Is Nothing Then
            'Ignore summary tasks - no assignments should be there
            If Not T.Summary Then
                'Loop through all assignments
                For Each At In T.Assignments
                    'Set R to resource of current assignment
                    Set R = P.Resources(At.ResourceID)
                    'Set ResourceAssignment to current TaskAssignment
                    
                    '****** INFORMATION ******************
                    'Unfortunatley there is an issue when UniqueIDs for assignments did
                    'not match between TaskAssignment and ResourceAssignment. If there is
                    'a fix in future enable "otion 1" instead of "option 2"
                    
                    'Start option 1 ****************************************************
    '                Set Ar = R.Assignments.UniqueID(At.UniqueID)
                    'End option 1 ****************************************************
                    
                    
                    'Start option 2 ****************************************************
                    For Each Ar In R.Assignments
                        If Ar.TaskID = T.ID Then
                            'now we have the correct resource assignment
                            Exit For
                        End If
                    Next Ar
                    'End option 2 ****************************************************
                    
                    'Copy value
                    Ar.MyResourceField = At.MyTaskField
                Next At
            End If
        End If
    Next T
    End Sub
    
    Sub CopyAssignmentFieldValueResourceToTask_ECF()
    'Copy assignment value from Task field "MyTaskField" to Resource field "MyResourceField" (in this sample)
    'Prerequisite:
    '   - For both fields, "Roll down unless manually specified" has to be enabled
    '   - Resource and Task field names must not contain any spaces (" ")
    
    
    Dim P As Project
    Dim T As Task
    Dim R As Resource
    Dim At As Assignment
    Dim Ar As Assignment
    
    
    'P is active Project
    Set P = ActiveProject
    
    'Loop through all resources
    For Each R In P.Resources
        'Ignore invalid resources
        If Not R Is Nothing Then
            'Loop through all assignments
            For Each Ar In R.Assignments
                'Consider only assignments for current project (there may by other projects open with the same resources)
                If Ar.Project = P Then
                    'Set task to current assignment task
                    Set T = P.Tasks(Ar.TaskID)
                    'Set TaskAssignment to current ResourceAssignment
                    
                    '****** INFORMATION ******************
                    'Unfortunatley there is an issue when UniqueIDs for assignments did
                    'not match between TaskAssignment and ResourceAssignment. If there is
                    'a fix in future enable "otion 1" instead of "option 2"
                    
                    'Start option 1 ****************************************************
    '                Set At = T.Assignments.UniqueID(Ar.UniqueID)
                    'End option 1 ****************************************************
                    
                    
                    'Start option 2 ****************************************************
                    For Each At In T.Assignments
                        If At.ResourceID = R.ID Then
                            'now we have the correct resource assignment
                            Exit For
                        End If
                    Next At
                    'End option 2 ****************************************************
    
                    'Copy value
                    At.MyTaskField = Ar.MyResourceField
                End If
            Next Ar
        End If
    Next R
    
    End Sub
    
Loading