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.
Dim sProj As Project
Dim mProj As Project
Set mProj = ActiveProject
For Each Subproject In mProj.Subprojects
Set sProj = ActiveProject
'for each task in sProj...
'do things to the tasks in your subproject
A couple of things to note here. First, I'm using the
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.