Search code examples
c#vbasharepoint-onlinems-project

VBA\C#.How to automatically map MS Project fields to a list on a SharePoint site


I have a list of tasks in MS project, which is synchronized with a SharePoint list created using a template, it has its own fields, for example “Description”, in project I have this field assigned to Text5. I have a bunch of such lists of 1 format but with different contents, and I need to do automatic field mapping using VB macro. (OR C#)

When I try to record a macro, I only get this code, and in the documentation I only saw setting the priority of the task

Sub Macro1()
    ManageSiteColumns
End Sub

Solution

  • To automate field mapping between Microsoft Project and a SharePoint list using VBA (Visual Basic for Applications), you can leverage the Text# fields in Microsoft Project to store custom data. Below is an example VBA code that you can use to map the "Description" field from SharePoint to the "Text5" field in Microsoft Project.

    Sub MapFields()
        Dim projApp As MSProject.Application
        Dim projTask As MSProject.Task
        Dim customFieldNumber As Integer
        
        ' Set the Microsoft Project Application
        Set projApp = GetObject(, "MSProject.Application")
        
        ' Check if Microsoft Project is open
        If projApp Is Nothing Then
            MsgBox "Microsoft Project is not open. Please open Microsoft Project and try again.", vbExclamation
            Exit Sub
        End If
        
        ' Specify the custom field number (Text5)
        customFieldNumber = 5
        
        ' Loop through each task in the active project
        For Each projTask In projApp.ActiveProject.Tasks
            ' Update the Text5 field with the value from the SharePoint "Description" field
            projTask.TextFields(customFieldNumber).Text = GetDescriptionFromSharePoint(projTask)
        Next projTask
        
        ' Clean up
        Set projApp = Nothing
    End Sub