Microsoft Project VBA: How to open a project file from Excel

Recently I’ve been playing around on how to open a Microsoft Project file from Excel using macros / VBA scripts.  I’m currently using MS Project 2010 and MS Excel 2010.  This script should work at minimum for MS Project 2010 / 2013.  If you need it for earlier version, it should be similar, so play around with the code.

You can build a procedure Sub in your VBA Editor after you add a new Module to your current workbook.

Before starting to write your code, make sure you have the right references to access Microsoft Project! Click on Tools>References>Microsoft Project xx.x Object Library. Then click okay.

Now you are ready to start coding in MS Excel to access information from MS Project!  Let’s start by opening an MS Project file from MS Excel.

Make sure to first set up your variables, which in this script you only need one.  You will need one variable for your MS Project application, which I called projApp.

I use the On Error Resume next in case MS Project is not installed and an error occurs.  This way the code stops running and you don’t get an error.  We set projApp to be a new  instance of object type.  This means we can now access everything we need in MS Project.

NOTE: Every time you run a new Macro /  VBA script you run a risk of losing all your work.  When trying out new code I recommend you use it on copies of your projects and not on active projects.  This way if your VBA doesn’t run correctly, you won’t lose valuable information.  Run it at your own risk!

Sub OpenProject()
Dim projApp As MSProject.Application


'Excel Set up to access MS Project
On Error Resume Next
Set projApp = GetObject(, "MSProject.Application")
If projApp Is Nothing Then
Set projApp = New MSProject.Application
End If
projApp.Visible = True


'Open MS Project file
projApp.Application.FileOpenEx "C:\<>\My Example Project.mpp" 

Set projApp = projApp.ActiveProject

'Final set up of code
Set projApp = Nothing

End Sub

Where you see “C:\<>\My Example Project.mpp”, this is where you put the information on where YOUR file is located.  I took out most of my file path to not make it confusing

The last comment I want to make is that about the MS Project file you might be trying to open.  I have found that if your MS Project file has macros, then you might have trouble using this macro because of Microsoft built in security. You might have to change the security settings so that your macro can work.

Otherwise, it works like a charm!



Leave a comment