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
 2
 3Application projectApplication = new MSProject.Application();
 4
 5object missingValue = System.Reflection.Missing.Value;
 6
 7//Open an MPP file
 8
 9projectApplication.FileOpenEx("Project1.mpp",
10
11    missingValue, missingValue, missingValue, missingValue,
12
13    missingValue, missingValue, missingValue, missingValue,
14
15    missingValue, missingValue, PjPoolOpen.pjPoolReadOnly,
16
17    missingValue, missingValue, missingValue, missingValue,
18
19    missingValue);
20
21//Create a Project object by assigning active project
22
23Project project = projectApplication.ActiveProject;
24
25//Loop through each task and read information related to tasks
26
27foreach (Task task in project.Tasks)
28
29{
30
31    Console.WriteLine("Reading Task " + task.Name);
32
33    Console.WriteLine("\nID: " + task.ID);
34
35    Console.WriteLine("Start: " + task.Start);
36
37    Console.WriteLine("Finish: " + task.Finish);
38
39    Console.WriteLine("\n===========================\n");
40
41    //Read any other information you need
42
43}
44
45//Loop through each resource and read information related to resources
46
47foreach (Resource resource in project.Resources)
48
49{
50
51    string resourceType = null;
52
53    switch (resource.Type)
54
55    {
56
57        case PjResourceTypes.pjResourceTypeCost:
58
59            resourceType = "Cost";
60
61            break;
62
63        case PjResourceTypes.pjResourceTypeMaterial:
64
65            resourceType = "Material";
66
67            break;
68
69        case PjResourceTypes.pjResourceTypeWork:
70
71            resourceType = "Work";
72
73            break;
74
75    }
76
77    Console.WriteLine("Reading Resource " + resource.Name);
78
79    Console.WriteLine("\nID: " + resource.ID);
80
81    Console.WriteLine("Type: " + resourceType);
82
83    Console.WriteLine("\n===========================\n");
84
85    //Read any other information you need
86
87}
88
89Console.ReadLine();

This example demonstrates how to use VSTO (Visual Studio Tools for Office) with the Microsoft Project Interop library to read tasks and resources from an MPP file. It starts by creating a new Application instance and opening a project file using FileOpenEx. The active project is accessed via ActiveProject, and then two loops iterate through the Tasks and Resources collections. For each task and resource, it prints details such as ID, name, start/finish dates, and resource type. This approach requires referencing the Microsoft Project 12.0 Object Library and using the Microsoft.Office.Interop.MSProject namespace.

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();
 2
 3Project project = reader.Read("Project.mpp");
 4
 5ArrayList allTasks =new ArrayList(project.RootTask.Children);
 6
 7foreach (Aspose.Tasks.Task task in allTasks)
 8
 9{
10
11    Console.WriteLine("Reading Task " + task.Name);
12
13    Console.WriteLine("\nID: " + task.Id);
14
15    Console.WriteLine("Start: " + task.Start);
16
17    Console.WriteLine("Finish: " + task.Finish);
18
19    Console.WriteLine("\n===========================\n");
20
21}
22
23
24
25//Loop through each resource and read information related to resources
26
27foreach (Resource resource in project.Resources)
28
29{
30
31    string resourceType = null;
32
33    switch (resource.Type)
34
35    {
36
37        case ResourceType.Material:
38
39            resourceType = "Material";
40
41            break;
42
43        case ResourceType.Work:
44
45            resourceType = "Work";
46
47            break;
48
49        default:
50
51            resourceType = "Cost";
52
53            break;
54
55    }
56
57
58
59    Console.WriteLine("Reading Resource " + resource.Name);
60
61    Console.WriteLine("\nID: " + resource.Id);
62
63    Console.WriteLine("Type: " + resourceType);
64
65    Console.WriteLine("\n===========================\n");
66
67}

This example shows how to read tasks and resources from a Microsoft Project file using Aspose.Tasks for .NET. First, a new Visual Studio project is created, and a reference to Aspose.Tasks is added via the .NET tab. The ProjectReader is used to load the MPP file, and all child tasks of the root task are iterated to display their details such as ID, name, start, and finish dates. Then, the code loops through project resources, identifies their type (Work, Material, or Cost), and outputs relevant information. This demonstrates a simple way to access and inspect project data using the Aspose.Tasks API.

Download Sample Code

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.