Project Management

Get started with Microsoft Project and Visual Basic programming

Andrew Makar goes back to basics and offers simple VBA examples that you can apply to Microsoft Project schedules.

I started fiddling with Visual Basic nearly 20 years ago in Mr. Desantis's Basic Programming class. Like most aspiring technologists, I found programming fun and, after learning I could get paid to work with computers, I jumped right in. Three years later, I was writing Visual Basic applications as an IT intern. Eventually, I handed in my programming hat for a project management role and started managing products rather than creating them. Even though I don't write code for a living, I still like to apply Visual Basic to my projects.

A quick Google search will reveal thousands of sites dedicated to VBA programming with Excel and Visual Basic. For the project management community, there isn't a lot of material available that provides realistic examples on how to apply Visual Basic with your Microsoft Project schedule. In my next several TechRepublic posts, I'll introduce you to some simple VBA examples that you can apply to your projects. Let's start with the ever appropriate Hello World example.

Step 1: Open Microsoft Project.

Create a sample schedule and save the file as HelloWorld.mpp. We will be using the schedule in future tutorials.

Step 2: Click the Developer tab and click the Visual Basic icon (Figure A). Figure A

Visual Basic Developer Tab

The Visual Basic window will open.

Step 3: Double-click the ThisProject(HelloWord.mpp) file (Figure B). Figure B

Visual Basic Editor (Click the image to enlarge.)

This is the blank code screen where you'll paint your Hello World masterpiece.

Step 4: Create the Hello World subroutine. Select Insert | Procedure, enter HelloWorld into the Name field, and select the Sub Type radio button (Figure C). Figure C

Add Procedure

Click OK, and the HelloWorld subroutine will appear.

Type the following code in the subroutine section;

MsgBox ("Hello World! This is my first VBA script in MS Project!")

Your one line subroutine should look like the code example below:

Step 5: Press F5 to Run the program (Figure D). Figure D

Hello World
The message box will display with the Microsoft Project file in the background. Once we know our simple script is working, we need to create a navigation bar so we can call it from the Microsoft Project Ribbon interface (Figure E). Figure E

Custom Utilities Navigation
Step 6: Create a subroutine to build the custom menu bar in the Ribbon UI.

1. Create a new subroutine called AddCustomNavigation.

2. Paste the following code into the subroutine:

Dim myNavBar As String
myNavBar = "<mso:customUI xmlns:mso=""http://schemas.microsoft.com/office/2009/07/customui"">"
myNavBar = myNavBar + "  <mso:ribbon>"
myNavBar = myNavBar + "    <mso:qat/>"
myNavBar = myNavBar + "    <mso:tabs>"
myNavBar = myNavBar + "      <mso:tab id=""tools"" label=""Utilities"" insertBeforeQ=""mso:TabFormat"">"
myNavBar = myNavBar + "        <mso:group id=""myTools"" label=""MyTools"" autoScale=""true"">"
myNavBar = myNavBar + "          <mso:button id=""placeholder1"" label=""Hello World"" "
myNavBar = myNavBar + "imageMso=""DiagramTargetInsertClassic"" onAction=""HelloWorld""/>"
myNavBar = myNavBar + "        </mso:group>"
myNavBar = myNavBar + "      </mso:tab>"
myNavBar = myNavBar + "    </mso:tabs>"
myNavBar = myNavBar + "  </mso:ribbon>"
myNavBar = myNavBar + "</mso:customUI>"
ActiveProject.SetCustomUI (myNavBar)

The Microsoft Project Developer Curriculum gets credit for providing an example on how to customize the Ribbon interface. As you read each line of the code, you'll notice the program is constructing an XML string that creates a tab called Utilities and a Group call MyTools. A button with the label HelloWorld is created, and the button's action is assigned the HelloWorld subroutine. The last line of the code -- ActiveProject.SetCustomUI (myNavBar) -- assigns the new tab to the navigation bar.

Step 7: Call the AddCustomNavigation subroutine when Microsoft Project opens.

The next step is to create a subroutine that calls the Add CustomNavigation subroutine. Select the Project and Open function from the Visual Basic editor window and paste the code below:

Private Sub Project_Open(ByVal pj As Project)
AddCustomNavigation
End Sub
Figure F depicts the entire code set. Figure F

Sample code (Click the image to enlarge.)
Step 8: Save the file and Exit Microsoft Project. Step 9. Re-open Microsoft Project. Click the Utilities tab, click the HelloWorld button, and celebrate your Microsoft Project programming success (Figure G). Figure G

Hello World final product (Click the image to enlarge.)

Next steps

Now that you have a working framework, you can start writing your own subroutines and add them to your menu bar. In case you've mistyped the code, you can download the sample code.

In the next couple of posts, I'll show how to use your Visual Basic skills to identify task missing baselines, identify late tasks, and a few other useful tricks with Microsoft Project with Visual Basic.

About

Dr. Andrew Makar is an IT program manager and is the author of How To Use Microsoft Project and Project Management Interview Questions Made Easy. For more project management advice visit http://www.tacticalprojectmanagement.com.

3 comments
Tony Hopkinson
Tony Hopkinson

You justified the existence of VB.net It's for people too scared to learn a new language...

Tommy Lovelace
Tommy Lovelace

I've used MSP since it's release in 1990 (Ver. 1.0). It's the most powerful scheduling program on earth. The ability to use Excel, MSP, or custom formulas between the fields puts it way above any product released by Primavera, Artemis, Open Plan, SureTrak...all of the others. This can all be done with or without Visual Basic. I suggest people learn about the unlimited capabilities of this "little" program. Tommy Lovelace Author of Tommy's Tips - Technical Advice Column in the Project Network News on the Engineering Applications of Microsoft Project

technomom_z
technomom_z

Microsoft Project AND Visual Basic? Wow. I love the '80s.