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
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:
Hello WorldThe 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 NavigationStep 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>"
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)
End SubFigure 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.)
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.