Microsoft Project VBA: How to Store Your Code by Importing and Exporting Modules

I frequently get questions on how I store my VBA code so I don’t accidentally lose it when  the Microsoft Project client decides to quit on me.  This is a good question since I have completely lost all my code in a project file and also while in the Global.MPT.  So how do you protect you protect your precious code?

The best way I found that works for me is exporting my code to a folder on my desktop or a hard drive every time I make a change to my code.  This means that some days I might make up to 15 copies of my code in an external folder so that I can always go back to see what changes I made along the way.  So how is it done?

Understanding MS Project VBA Editor

Before we start exporting data from the VBA Editor in MS Project, let’s explore where we can find this environment. One way to get to the VBA Editor from MS Project window is by clicking Alt + F11.  Another way  is to go to the View tab in your ribbon and click the upside down triangle on the Macros section.  From there choose Visual Basic.

MSP Ribbon to Visual Basic Editor

Visual Basic Editior (VBE)

Once you get to the Visual Basic Editor (VBE) you can start coding.  I recommend you save your code all the time, especially before running it if you are constantly making changes.  After a couple of lines of code, you might want to consider exporting your data to safeguard it outside of MS Project so no matter how many times it crashes you can always bring it back.

Let’s first take a look at the VBE.  In there you will notice at least two main areas (three if you are connected to MS Project Server).  Since on my personal laptop I don’t have access to MS Project Server, I only have two main areas.  You can see these areas usually to the left top of your VBE.  If you can’t see it go to your View menu and click on Project Explorer or do CTRL + R.

MSP VBE Project Explorer

Once you can see your Project Explorer, then you will be able to see the two main areas in a tree structure.

The first main node is the Project Global (Global.MPT).  The second main node will be your currently opened project.  If you have more than one project file open, then you will see one node per project.  The project file node is the one with VBAProject (<project file name>). Since I only have one project open I only see two main nodes.  (For those of you with MS Project Server, the third main node or bottom node will be a Global Enterprise node.  This is where all the code is kept for the whole enterprise/company).

Within each node you will find different types of sections for modules, class modules, and user forms as your project requires.

VBA Environment v1

Exporting MS Project VBA

Now that we have identified the two main areas in the Project Explorer, now we are ready to export our code.  I want to export Module1 from my opened project file.

  • Navigate VBAProject(MG VBA Macros) > Modules > Module1
  • Right-click on Module1 and choose Export File

MSP VBE Export

  • Choose where you want to save your code and click Save.  This will export your code as a .BAS extension file.

MSP VBE Export Save

And that’s it!  Your wonderful code is saved!  Now if MS Project crashes or something weird happens you will have your code somewhere else.

What I love about having the code exported this way instead of highlighting it all and pasting to Notepad (or any other editor) is that the code is properly formatted.  So when you try to imported back into MS Project it works perfectly.  I sometimes have trouble with the code when I copy / paste directly to notepad… it can create errors once you try to paste it back into MS Project.

Importing MS Project VBA

So now you may be asking yourself, how do we import the code back in?  Good question!

To demonstrate I deleted the previous Module1 from my project.  Now I want to bring it back into my project by importing it.

  • Right-click on VBAProject(<your project name>)
  • Click on Import File
  • Navigate to the folder where you keep your code
  • Click on the file with your code and click Open

MSP VBE ImportMSP VBE Import Open

Then you are done!  You have Exported and Imported your code in MS Project!

Until next time… 🙂



Leave a comment