Reading Tasks and Resources in VSTO and Aspose.Tasks

Code Examples

VSTO

The following steps are required to accomplish this task:

  1. Create a new project in Visual Studio.
  2. In the Solution Explorer, right-click and select Add Reference, then select the COM components tab.
  3. Select Microsoft Project 12.0 Object Library and click OK.
  4. This imports the Microsoft.Office.Interop.MSProject namespace at the start of the code.Use the code from the following example to read tasks and resources.
 1//Create Application object
 2Application projectApplication = new MSProject.Application();
 3object missingValue = System.Reflection.Missing.Value;
 4//Open an MPP file
 5projectApplication.FileOpenEx("Project1.mpp",
 6    missingValue, missingValue, missingValue, missingValue,
 7    missingValue, missingValue, missingValue, missingValue,
 8    missingValue, missingValue, PjPoolOpen.pjPoolReadOnly,
 9    missingValue, missingValue, missingValue, missingValue,
10    missingValue);
11//Create a Project object by assigning active project
12Project project = projectApplication.ActiveProject;
13//Loop through each task and read information related to tasks
14foreach (Task task in project.Tasks)
15{
16    Console.WriteLine("Reading Task " + task.Name);
17    Console.WriteLine("\nID: " + task.ID);
18    Console.WriteLine("Start: " + task.Start);
19    Console.WriteLine("Finish: " + task.Finish);
20    Console.WriteLine("\n===========================\n");
21    //Read any other information you need
22}
23//Loop through each resource and read information related to resources
24foreach (Resource resource in project.Resources)
25{
26    string resourceType = null;
27    switch (resource.Type)
28    {
29        case PjResourceTypes.pjResourceTypeCost:
30            resourceType = "Cost";
31            break;
32        case PjResourceTypes.pjResourceTypeMaterial:
33            resourceType = "Material";
34            break;
35        case PjResourceTypes.pjResourceTypeWork:
36            resourceType = "Work";
37            break;
38    }
39    Console.WriteLine("Reading Resource " + resource.Name);
40    Console.WriteLine("\nID: " + resource.ID);
41    Console.WriteLine("Type: " + resourceType);
42    Console.WriteLine("\n===========================\n");
43    //Read any other information you need
44}
45Console.ReadLine();

Aspose.Tasks

The following steps are required to accomplish this task:

  1. Create a new project in Visual Studio.
  2. In the Solution Explorer, right-click and select Add Reference, then select the .NET tab.
  3. Select Aspose.Tasks and then click OK.
  4. This imports the Aspose.Tasks namespace at the start of the code.Use the code from the following example to read tasks and resources.
 1ProjectReader reader = new ProjectReader();
 2Project project = reader.Read("Project.mpp");
 3ArrayList allTasks =new ArrayList(project.RootTask.Children);
 4foreach (Aspose.Tasks.Task task in allTasks)
 5{
 6    Console.WriteLine("Reading Task " + task.Name);
 7    Console.WriteLine("\nID: " + task.Id);
 8    Console.WriteLine("Start: " + task.Start);
 9    Console.WriteLine("Finish: " + task.Finish);
10    Console.WriteLine("\n===========================\n");
11}
12
13//Loop through each resource and read information related to resources
14foreach (Resource resource in project.Resources)
15{
16    string resourceType = null;
17    switch (resource.Type)
18    {
19        case ResourceType.Material:
20            resourceType = "Material";
21            break;
22        case ResourceType.Work:
23            resourceType = "Work";
24            break;
25        default:
26            resourceType = "Cost";
27            break;
28    }
29
30    Console.WriteLine("Reading Resource " + resource.Name);
31    Console.WriteLine("\nID: " + resource.Id);
32    Console.WriteLine("Type: " + resourceType);
33    Console.WriteLine("\n===========================\n");
34}

Download Sample Code

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.