Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter through tasks and retrieve names

Tags:

c#

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.

like image 531
NickeNyfiken Avatar asked Nov 10 '22 13:11

NickeNyfiken


1 Answers

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);
             }
     }
 }
like image 117
darkstar3d Avatar answered Nov 14 '22 23:11

darkstar3d