« In Houston | Main | Two Landscapes »

Iterating through Microsoft Project Subprojects

Master Projects in Microsoft Project don't always behave the way you expect, especially if you are trying to iterate through all the tasks in them. The reason is that the file doesn't really contain the subprojects, it merely acts as a shell to display them. So if you are writing some code which has to work on a master project you need to take this into account.

The simplest explanation is an example. The code below will start with a master project, then find the list of subprojects, open each one in turn and display a message box with the project name. You can of course do whatever you want with the subproject as soon as you have it open.

Sub openMySubProjects()
Dim sProj As Project
Dim mProj As Project
Set mProj = ActiveProject
For Each Subproject In mProj.Subprojects
FileOpen (Subproject.Path)
Set sProj = ActiveProject
MsgBox sProj.Name
'for each task in sProj...
'do things to the tasks in your subproject
'next task
Next Subproject
mProj.Activate
End Sub

A couple of things to note here. First, I'm using the mProj and sProj variables to reference the different projects I have open. We want to return to the master project at the end so I set mProj equal to the ActiveProject when we start and then return to it by calling mProj Activate at the end.

The second thing is that the subproject object only has a few properties. It is a reference to a project and not a project itself. We use the Path of the subproject to open the file that it is referring to and then use sProj to refer to that file once it is open. Just to be sure you get the point, subprojects point to the file, we are using a project variable to refer to that file when it is opened. You can't get typical project properties (not even Name) from subprojects. You only use the subproject object to get to the project.

It goes without saying that this is a trivial example. You should probably close it once you are done with it, and maybe even validate that it exists before you do anything with it, but I just wanted to make this as simple to follow as I possibly can.

RELATED POSTS
  • Setting Microsoft Project Level Custom fields using VBA
  • Microsoft Project Undo Levels and Macros
  • VBA to VSTO Tutorial Part Two - Adding a Command Bar and Buttons
  • Using a ComboBox in a Microsoft Project Userform
  • VBA and Visual Basic For ... to ... statements
  • Office VBA for Mac After 2008
  • Analyze Microsoft Project Resource Usage Data In Excel
  • VBA Writing to a text file (MS Project, Excel)
  • MS Project VBA - Trim Function
  • Telling Time - ProjDateDiff, VB DateDiff and Application.DateDifference

  • About

    The previous article is In Houston.

    The next article is Two Landscapes.

    Current articles are in the main index page and you can find a complete list of articles in the archives.

    Creative Commons License
    This weblog is licensed under a Creative Commons License.
    Powered by
    Movable Type 3.34