VBA Project

Project is a Microsoft application used for project management. VBA is embedded in the Project application and can used to work with Project programmatically. The Project Object Library contains classes that make working with Project in VBA possible. The classes in the Project Object Library are referred to collectively as the Project Object Model. When using VBA within Project, a reference is automatically included to the Project Object Library.

Project Object Model

The Project Object Model contain classes that make working with Project in VBA possible. The Project Object Model contains many classes but the general overview of the model is Application → Project → Task.

  1. Application
    1. Projects
      1. Project
        1. Tasks
          1. Assignments
            1. Assignment
        2. Calendars
          1. Calendar
        3. Resources
          1. Resource

Context

The place where VBA code is written determines the implied context when referring to classes. For example, when using VBA in Project and referring to the Application object, it is implied that Application and MSProject.Application are the same object. However, when using VBA in another application to work with Project, it is necessary to qualify references to avoid naming collisions with classes in the other application. Using qualified references requires that a reference is set to the library where the classes are defined. If a reference cannot be set, late-binding can be used instead.

Public Sub Example()
    
    '''In Project

    Debug.Print Application Is MSProject.Application 'Prints: True

End Sub
Public Sub Example()
    
    '''In Excel

    'Reference to Project Object Library is required

    Debug.Print Application Is MSProject.Application 'Prints: False
    
    Debug.Print Application Is Excel.Application 'Prints: True

End Sub

To retrieve the Project Application object from another application, attempt to retrieve the running application and if that fails start a new instance of the application.

Public Function GetProjectApp() As Object

    On Error Resume Next
    Set GetProjectApp = GetObject(, "MSProject.Application")
    On Error GoTo 0

    If GetProjectApp Is Nothing Then
        Set GetProjectApp = CreateObject("MSProject.Application")
    End If

End Function

Application

The Application object represents the Project application itself. The Application object is the top-level object and provides access to the rest of the Project Object Model. The Application object can be used to access functionality and properties of the Project application.

Projects

The Project object represents a Project. The Projects collection object contains a collection of Project objects. Use the Application.Projects property to get the Projects collection containing all open Projects.

Iterate Projects

To iterate over all open projects use a For Each loop with the Application.Projects property.

Public Sub Example()

    Dim P As MSProject.Project

    For Each P In MSProject.Application.Projects
        Debug.Print P.Name
    Next P

End Sub

Add Project

To add a project use the Projects.Add method.

Public Sub Example()

    Dim P As MSProject.Project
    Set P = MSProject.Application.Projects.Add

End Sub

Save Project

To save a project use the SaveAs method. Use the PjFileFormat enum to determine the file format.

Public Sub Example()

    Dim P As MSProject.Project
    Set P = MSProject.Application.Projects.Add

    P.SaveAs Environ$("USERPROFILE") & "\Desktop\ExampleProject.mpp", pjMPP

End Sub

Export as Fixed Format

To export a project as a PDF or XPS format file use the ExportAsFixedFormat method. Use the PjDocExportType enum to determine the file type.

Public Sub Example()

    Dim P As MSProject.Project
    Set P = MSProject.Application.Projects(1)

    Dim FilePath As String
    FilePath = Environ$("USERPROFILE") & "\Desktop\ExampleProjectPDF.pdf"

    P.ExportAsFixedFormat FilePath, pjPDF

End Sub

Tasks

The Task object represents a task in a Project. The Tasks collection object contains a collection of Task objects. Use the MSProject.Tasks property to get a Tasks collection containing all Task objects in a Project.

Iterate Tasks

To loop over tasks use a For Each loop with the MSProject.Tasks property.

Public Sub Example()

    Dim P As MSProject.Project
    Set P As MSProject.Application.Projects(1)

    Dim T As MSProject.Task
    For Each T In P.Tasks
        Debug.Print T.Name
    Next T

End Sub

Add Task

To add a task use the Tasks.Add method.

Public Sub Example()

    Dim P As MSProject.Project
    Set P As MSProject.Application.Projects(1)

    Dim T As MSProject.Task
    Set T = P.Tasks.Add

End Sub

Delete Task

To delete a task use the Delete method.

Public Sub Example()

    Dim P As MSProject.Project
    Set P As MSProject.Application.Projects(1)

    P.Tasks(1).Delete

End Sub