none
[VSTO] Max OutlineLevel and list all tasks with required properties RRS feed

  • Question

  • Hi,

    I'm developing an AddIn (VSTO) for Microsoft Project 2010 Professional. 

    1. Each Task object has his OutlineLevel. To get max. OutlineLevel, I have to loop all taks, read this property and make comparison. Is there another way to read max. OutlineLevel of project without looping? I would like now how deep is the structure.

    2. I also need to read Start/Finish date of all tasks. Is there any way to read them without looping through all tasks? I would like to improve the performance because the loop is pretty slow because of accessing COM object. :(.

    Thank you,



    • Edited by Tri D Nguyen Wednesday, October 23, 2013 2:23 PM
    Wednesday, October 23, 2013 2:22 PM

All replies

  • Tri,

    I've never worked with VSTO but in VBA looping through all tasks in background mode, (i.e. For Each t in ActiveProject.Tasks), goes very fast. In background mode (i.e. For Each t in ActiveSelection.Tasks), processing may be a bit slower because it is operating on objects in the current view instead of objects directly in Project's database (i.e. screen updates chew up processor time).

    Nonetheless, you might want to play with this idea. Instead of looping through the whole file to find the max outline level, make an assumption as to what you think it might be (e.g. 5 levels). Then set up a filter for all tasks with outline level 6 or more. Do an active selection and if there are no tasks then you know that the max outline level is less than 6. If there are tasks, you can up the filter value for an outline level of 7 and go again. If a filter with outline level of 6 gave nothing, then decrease the outline level until you find tasks. It is an iterative process and counter to what I said earlier about background processing being faster than foreground processing but in some cases a foreground processing technique may in fact be quicker than looping through the whole file using background processing.

    With regard to your second question, I know of no other way to get all the start and finish dates, (or any other data for that matter), than reading it in a loop. If you were using Project Server, there might be some table that has the data in an easy to get form but since I don't use Project Server, I don't know.

    Hope this helps.

    John

    Wednesday, October 23, 2013 7:48 PM
  • Hi John,

    In VSTO or in .Net Application, looping through all tasks are pretty slow. I don't have any other solution where I don't have to loop through.

    However using Parallel will speed up the looping process and it costs only about 30% running time in comparison to normal loop.

    var tasks = project.Tasks;
    int tasksCount = tasks.Count;
    int maxOutlineLevel = 0;
    IList<ProjectTaskInfo> taskInfos = new List<ProjectTaskInfo>(new ProjectTaskInfo[tasksCount]);
    
    Parallel.For(0, tasksCount, index =>
    {
    	DateTime tempDateTime = new DateTime();
    	MSProject.Task task = tasks[index + 1];
    	if (task != null)
    	{
    		taskInfos[index] = new ProjectTaskInfo();
    		taskInfos[index].Name = task.Name;
    		taskInfos[index].OutlineLevel = task.OutlineLevel;
    		taskInfos[index].ParentId = task.OutlineParent.UniqueID;
    		taskInfos[index].UniqueId = task.UniqueID;
    		if (DateTime.TryParse(task.Start.ToString(), out tempDateTime))
    			taskInfos[index].Start = tempDateTime;
    		if (DateTime.TryParse(task.Finish.ToString(), out tempDateTime))
    			taskInfos[index].Finish = tempDateTime;
    		if (string.IsNullOrWhiteSpace(task.Guid))
    		{
    			taskInfos[index].Guid = "VERSION_2000_2003";
    		}
    		else
    		{
    			taskInfos[index].Guid = task.Guid.ToString();
    		}
    
    		maxOutlineLevel = Math.Max(maxOutlineLevel, task.OutlineLevel);
    		task.Dispose();
    	}
    });
    
    tasks.Dispose();
    project.Dispose();

    Thursday, November 7, 2013 1:38 PM
  • Tri,

    I've never heard of the Parallel function. Apparently it is not something that is available in VBA and I don't remember hearing about it in C when I learned about C programming many many years ago. But, if you found something that works and meets your needs, then give yourself credit for answering your own question.

    John

    Thursday, November 7, 2013 3:47 PM