Adding Links for Tasks in VSTO and Aspose.Tasks
Code Examples
VSTO
To link a task using VSTO:
Create a new project in Visual Studio.
In the Solution Explorer, right-click and select Add Reference.
Select the COM components tab, and select Microsoft Project 12.0 Object Library.Click OK.This imports the Microsoft.Office.Interop.MSProject namespace at the start of your code. Use the code from the following example to link tasks.
1Microsoft.Office.Interop.MSProject.Application projectApplication = new MSProject.Application();
2
3object missingValue = System.Reflection.Missing.Value;
4
5projectApplication.FileOpenEx("Project.mpp",
6
7 missingValue, missingValue, missingValue, missingValue,
8
9 missingValue, missingValue, missingValue, missingValue,
10
11 missingValue, missingValue, PjPoolOpen.pjPoolReadOnly,
12
13 missingValue, missingValue, missingValue, missingValue,
14
15 missingValue);
16
17Microsoft.Office.Interop.MSProject.Project project = projectApplication.ActiveProject;
18
19project.Tasks.get_UniqueID(2).TaskDependencies.Add(project.Tasks.get_UniqueID(1), PjTaskLinkType.pjFinishToStart);
20
21project.Tasks.get_UniqueID(3).TaskDependencies.Add(project.Tasks.get_UniqueID(2), PjTaskLinkType.pjFinishToStart);
22
23project.Tasks.get_UniqueID(4).TaskDependencies.Add(project.Tasks.get_UniqueID(3), PjTaskLinkType.pjFinishToStart);
24
25project.Tasks.get_UniqueID(5).TaskDependencies.Add(project.Tasks.get_UniqueID(4), PjTaskLinkType.pjFinishToStart);
26
27project.Tasks.get_UniqueID(5).TaskDependencies.Add(project.Tasks.get_UniqueID(2), PjTaskLinkType.pjFinishToStart);
28
29foreach (Task tsk in project.Tasks)
30
31{
32
33 foreach (TaskDependency dep in project.Tasks.get_UniqueID(tsk.ID).TaskDependencies)
34
35 {
36
37 Console.WriteLine("From ID = " + dep.From.ID + "=>To ID = " + dep.To.ID);
38
39 }
40
41 Console.WriteLine("____________________________________________________________");
42
43}
44
45projectApplication.FileCloseAll(Microsoft.Office.Interop.MSProject.PjSaveType.pjSave);
This C# code uses Microsoft Project Interop to open an existing .mpp
file and programmatically add task dependencies. First, it initializes an instance of Microsoft Project via Application
and opens the file "Project.mpp"
in read-only pool mode. Then, it retrieves the active project and defines several Finish-to-Start dependencies between tasks based on their unique IDs. Specifically, task 2 is set to depend on task 1, task 3 on task 2, task 4 on task 3, and task 5 on both task 2 and task 4. After setting up these dependencies, the code iterates through all tasks in the project and prints out the defined dependency relationships to the console, showing which tasks are predecessors and successors. Finally, it saves and closes the project file. This code demonstrates how developers can automate the configuration of task relationships in Microsoft Project using COM Interop, which requires Microsoft Project to be installed on the system.
Aspose.Tasks
To link tasks in a project using Aspose.Tasks for .NET:
Create a new project in Visual Studio.
In the Solution Explorer, right-click and select Add Reference.
Select .NET tab and select Aspose.Tasks.Click OK.This imports the Aspose.Tasks namespace at the start of your code. Use the code from the following example to link tasks.
1ProjectReader reader = new ProjectReader();
2
3FileStream St = new FileStream("Project.mpp", FileMode.Open);
4
5Project prj = reader.Read(St);
6
7St.Close();
8
9TaskLink taskLink = new TaskLink(prj.RootTask.Children[0] as Aspose.Tasks.Task, prj.RootTask.Children[1] as Aspose.Tasks.Task, TaskLinkType.FinishToStart);
10
11prj.AddTaskLink(taskLink);
12
13taskLink = new TaskLink(prj.RootTask.Children[1] as Aspose.Tasks.Task, prj.RootTask.Children[2] as Aspose.Tasks.Task, TaskLinkType.FinishToStart);
14
15prj.AddTaskLink(taskLink);
16
17taskLink = new TaskLink(prj.RootTask.Children[2] as Aspose.Tasks.Task, prj.RootTask.Children[3] as Aspose.Tasks.Task, TaskLinkType.FinishToStart);
18
19prj.AddTaskLink(taskLink);
20
21taskLink = new TaskLink(prj.RootTask.Children[3] as Aspose.Tasks.Task, prj.RootTask.Children[4] as Aspose.Tasks.Task, TaskLinkType.FinishToStart);
22
23prj.AddTaskLink(taskLink);
24
25taskLink = new TaskLink(prj.RootTask.Children[1] as Aspose.Tasks.Task, prj.RootTask.Children[4] as Aspose.Tasks.Task, TaskLinkType.FinishToStart);
26
27prj.AddTaskLink(taskLink);
28
29ArrayList allLinks = new ArrayList(prj.TaskLinks);
30
31foreach (TaskLink taskLink1 in allLinks)
32
33{
34
35 Console.WriteLine("From ID = " + taskLink1.PredTask.Id + "=>To ID = " + taskLink1.SuccTask.Id);
36
37 Console.WriteLine("________________________________________");
38
39}
40
41prj.Save("Project1.mpp", Aspose.Tasks.Saving.SaveFileFormat.MPP);
This C# code demonstrates how to use Aspose.Tasks for .NET to add task dependencies (task links) to an existing Microsoft Project file (.mpp
) and save the updated result. It starts by opening the file "Project.mpp"
using a FileStream
and reading it with ProjectReader
. The code then programmatically creates several Finish-to-Start dependencies between consecutive tasks (from the first to the fifth child task), as well as one additional dependency between the second and fifth tasks. Each dependency is represented by a TaskLink
object, which is added to the project using AddTaskLink()
. After all dependencies are added, the code collects all task links into an ArrayList
and prints each relationship to the console by displaying the predecessor and successor task IDs. Finally, the project is saved to a new file "Project1.mpp"
in Microsoft Project format.