1

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

1 Answer 1

1

Use the Text# fields in Microsoft Project to store custom data. Here is an example that maps "Description" 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

To get the description from SharePoint in VBA it is not straightforward due to the limitations of VBA in interacting with SharePoint's API. However, you can use a workaround by mapping the SharePoint folder to a drive letter.

Option Explicit

Private oMappedDrive As Scripting.Drive
Private oFSO As New Scripting.FileSystemObject
Private oNetwork As New WshNetwork

Private Sub Class_Terminate()
    UnmapDrive
End Sub

Public Function MapDrive(NetworkPath As String) As Scripting.Folder
    Dim DriveLetter As String, i As Integer

    UnmapDrive

    For i = Asc("Z") To Asc("A") Step -1
        DriveLetter = Chr(i)
        If Not oFSO.DriveExists(DriveLetter) Then
            oNetwork.MapNetworkDrive DriveLetter & ":", NetworkPath
            Set oMappedDrive = oFSO.GetDrive(DriveLetter)
            Set MapDrive = oMappedDrive.RootFolder
            Exit For
        End If
    Next i
End Function

Private Sub UnmapDrive()
    If Not oMappedDrive Is Nothing Then
        If oMappedDrive.IsReady Then
            oNetwork.RemoveNetworkDrive oMappedDrive.DriveLetter & ":"
        End If
        Set oMappedDrive = Nothing
    End If
End Sub

Try this and let me know if it helps.

1
  • 1
    Hello, thank you for the work done, there is no way to check now. But due to the fact that we at the company could not find an answer to our question on the Internet, we decided to write a program in C# that will synchronize sp with mpp. Commented Jan 6, 2024 at 0:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.