5

this is my first question on the site although I have gotten lots of help from it already.

What I am looking to do is to open a MSProject (.MPP) document and look through all the task names, then only retrieve the ones with a specified name.

I have found a bit of code on this site NerdyHearn which seems to suit my needs, but for some reason I can only retrieve the name of the absolute first task? Which seems strange to me since this line of code here:

    foreach (Microsoft.Office.Interop.MSProject.Task task in proj.Tasks)
    {
    }

Is a loop right? I have tried with different .MPP files and even created my own, but it still only retrieves the first one.

And if someone can help me to design a filter to use when retrieving the task names that would be great!

I have also tried to find documentation for Microsoft.Office.Interop.MSProject but I really can't seem to find it.

5
  • Maybe the Task class has a collection of subtasks and there are the task you are looking for
    – Ivo
    Feb 8, 2014 at 3:43
  • I think you may be on to something, I think that using task.Name is wrong, for example if you write Console.WriteLine(proj.NumberOfTasks); it displays '89' which is right. Now I only need some way of actually making it print out the names. You can print out other tasks if you manually add successors in Microsoft Project and then write Console.WriteLine(task.SuccessorTasks); but then we would have to back track too far.. Feb 9, 2014 at 21:31
  • it works for me, but I tried on a very simple file. Can you share one of the mpp files that gives you the problem? Feb 18, 2014 at 11:24
  • I'm not following either. Using the code provided, except that I used Application projApp = new Application(); instead of ApplicationClass, I get tasks and their subs. Task.Name is correct (for me). A file would definitely be helpful.
    – darkstar3d
    Feb 18, 2014 at 22:24
  • Not sure and just as idea, try to get the Name using the GetField property instead of Name property such as: task.GetField(PjField.pjTaskName)
    – tatigo
    Feb 18, 2014 at 23:34

1 Answer 1

1
+50

Posting the code block so that you see what I have. Please post a file if you could, perhaps its something else instead of subtasks as I have 4 levels in some spots and all my tasks come out as expected.

using Microsoft.Office.Interop.MSProject;
using System;
using System.Reflection;
namespace PProject
{
    public class Program
    {
         public static void Main()
         {

             Application projApp = new Application();
             projApp.FileOpenEx(@"C:\Tickets.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, Missing.Value);

             Project proj = projApp.ActiveProject;

             // Enumerate the tasks
             foreach (Task task in proj.Tasks)
             {
                 string name = task.Name;

                 Console.WriteLine(task.Name);
             }

             // Make sure to clean up and close the file
             projApp.FileCloseAll(PjSaveType.pjDoNotSave);
             }
     }
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.