Question Opening MS Project 2007


Sep 3, 2008
Programming Experience
I am developing a Windows Forms application in VB.NET using Visual Studio 2008. I am attempting to open an MSProject.mpp file and iterate through the tasks. Here is my code from Form1 so far:

Dim mFileName As String
'When looking at the project Window drop down, determine the index number of the file
Dim iFileIndex As Int16
Dim pjApplication As New Microsoft.Office.Interop.MSProject.Application
Dim mTasks As Microsoft.Office.Interop.MSProject.Tasks
Dim mTask As Microsoft.Office.Interop.MSProject.Task
'Get an mpp file, no error checking at this time

mFileName = OpenFileDialog1.FileName.ToString()
TextBox1.Text = mFileName.ToString

pjApplication.FileOpen(mFileName) 'FileOpenEx(SourceFile) ????
pjApplication.Visible = True

Debug.Print("File Opened: " & mFileName.ToString)

iFileIndex = pjApplication.ActiveProject.Index
Debug.Print("Active Project: " & pjApplication.ActiveProject.Name.ToString)
mTasks = pjApplication.ActiveProject.Tasks
Debug.Print("Task Count: " & mTasks.Count.ToString)
Debug.Print("Source File Index: " & iFileIndex.ToString & " Task Count: " & mTasks.Count)
'************************************************* *******
'This is where it dies: it tells me mTask is set to "Nothing" What do I need??
'************************************************* ********
For Each mTask In mTasks 'pjApplication.ActiveProject.Tasks
'the real work goes here
Next mTask

So it does not like the For Each loop for some reason. It is the mTask that seems to be doing it. All the Debug.Print statements return expected values.

What do I need to assign to mTask to get this to work?
Top Bottom