Reading Tasks and Resources in VSTO and Aspose.Tasks
Contents
[
Hide
Show
]Code Examples
VSTO
The following steps are required to accomplish this task:
- Create a new project in Visual Studio.
- In the Solution Explorer, right-click and select Add Reference, then select the COM components tab.
- Select Microsoft Project 12.0 Object Library and click OK.
- 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();
Aspose.Tasks
The following steps are required to accomplish this task:
- Create a new project in Visual Studio.
- In the Solution Explorer, right-click and select Add Reference, then select the .NET tab.
- Select Aspose.Tasks and then click OK.
- 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}