Home
Blog
Contact
Mailing List
Software
Active Directory Products
Object Compare
Permission Compare
IPhone Products
Calls To Calendar
SMS To Gmail
Voicemail To Gmail
Sites
SaveMySerials
How Long For Me
DocuTerminal
My Music To Me
Blog
Twitter
|
<< Back To All Blogs
Reading a Microsoft Project file (mpp) in C#
Wednesday, June 24th, 2009
I was recently presented with the need to read the tasks from a Microsoft Project file and to my surprise there is not a large number of information on doing so.
There is an open source project on source forge called MPJX which claims to support .NET as well, but upon further investigation I realized that it was actually embedding a virtual Java processor in .NET and then calling
the Java-native version... yuck.
So I decided the way I would go is to use .NET Interop to invoke Microsoft Project and pull the information I required. The only downside to this approach is that you most have Project installed on the box, but for me this was not an issue.
You will need to add a reference to Microsoft Project in your Visual Studio project, and it will be under the COM tab, not under the .NET tab.
So without further ado, here is how I went about reading a Microsoft Project file in C#:
using Microsoft.Office.Interop.MSProject;
using System.Reflection;
ApplicationClass projectApp = new ApplicationClass();
// Open the file. There are a number of options in this constructor as you can see
projectApp.FileOpen("C:\myfile.mpp", true, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
PjPoolOpen.pjDoNotOpenPool, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
// Get the active project
Project proj = projectApp.ActiveProject;
// Enumerate the tasks
foreach (Task task in proj.Tasks)
{
string name = task.Name;
// Project stores the number of minutes in a workday, so 8 hours per workday * 60 = 480. 480 is a project "day"
int duration_in_days = Int32.Parse(task.Duration.ToString()) / 480;
DateTime start = DateTime.Parse(task.Start.ToString());
DateTime finish = DateTime.Parse(task.Finish.ToString());
double percent_complete = Double.Parse(task.PercentComplete.ToString());
DateTime actual_finish = DateTime.Parse(task.ActualFinish.ToString());
// Do something with each task here
}
// Make sure to clean up and close the file
projectApp.FileCloseAll(PjSaveType.pjDoNotSave);
A pretty simple example, but I could not find a single example of doing this online.
Projectin' Tom Out.
Tags
Howto
CSharp
Related Blogs
Fixing Virtual PC 2007 To Run On Windows 7
Search and Replace in MySQL
Setting up a real VPN with your every day consumer router and DD-WRT
Resolving ASP.NET Web.Config Inheritance
Resolving Avaya Denial Event 2378
Comments
Asit Aithal said on Friday, January 8th, 2010 @ 8:29 AM
Great stuff. How can I iterate thru' all the fields and display the values?
Tom said on Tuesday, November 17th, 2009 @ 12:44 PM
Hi Rob,
Glad to hear it helped, I myself had quite a tough time finding documentation during the process.
Cheers!
Tom
rob said on Tuesday, November 17th, 2009 @ 12:37 PM
Thanks! This saved me a tonne of time. You are correct in that there isn't much documentation out there regarding opening MS Project files using interop.
Tom said on Sunday, October 11th, 2009 @ 6:02 PM
Hi Hugonne,
I never had to try using the Interop assembly without Project installed, so I'm not sure. This is probably a possibility but you would have to be careful with licensing. You may also have to fight with a number of system DLL dependencies as I'm sure Project has a number of them if it is anything like the other Office Products.
Hugonne said on Wednesday, October 7th, 2009 @ 3:50 PM
My bad, I just read that indeed you have to have it installed. Too bad. Could there be a way to deploy with just the Interop assembly, so thah Project isn't needed? Thanks.
Hugonne said on Wednesday, October 7th, 2009 @ 3:48 PM
Hey, great article. I have a question though: When this application gets deployed, does the production machine need a copy of Project installed? See, we're developing an utility to load to a Sql Server db Excel and Project files, but we wouldn't like to have Project (or Excel, but that can be solved with ODBC) installed on the production server. Thanks.
Add A Comment
Name:
URL:
Email Address: (not public, used to send notifications on further comments)
Comments:

Enter the text above, except for the 1st and last character:
|