Adding Links for Tasks in VSTO and Aspose.Tasks
Contents
[
Hide
Show
]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();
2object missingValue = System.Reflection.Missing.Value;
3projectApplication.FileOpenEx("Project.mpp",
4 missingValue, missingValue, missingValue, missingValue,
5 missingValue, missingValue, missingValue, missingValue,
6 missingValue, missingValue, PjPoolOpen.pjPoolReadOnly,
7 missingValue, missingValue, missingValue, missingValue,
8 missingValue);
9Microsoft.Office.Interop.MSProject.Project project = projectApplication.ActiveProject;
10project.Tasks.get_UniqueID(2).TaskDependencies.Add(project.Tasks.get_UniqueID(1), PjTaskLinkType.pjFinishToStart);
11project.Tasks.get_UniqueID(3).TaskDependencies.Add(project.Tasks.get_UniqueID(2), PjTaskLinkType.pjFinishToStart);
12project.Tasks.get_UniqueID(4).TaskDependencies.Add(project.Tasks.get_UniqueID(3), PjTaskLinkType.pjFinishToStart);
13project.Tasks.get_UniqueID(5).TaskDependencies.Add(project.Tasks.get_UniqueID(4), PjTaskLinkType.pjFinishToStart);
14project.Tasks.get_UniqueID(5).TaskDependencies.Add(project.Tasks.get_UniqueID(2), PjTaskLinkType.pjFinishToStart);
15foreach (Task tsk in project.Tasks)
16{
17 foreach (TaskDependency dep in project.Tasks.get_UniqueID(tsk.ID).TaskDependencies)
18 {
19 Console.WriteLine("From ID = " + dep.From.ID + "=>To ID = " + dep.To.ID);
20 }
21 Console.WriteLine("____________________________________________________________");
22}
23projectApplication.FileCloseAll(Microsoft.Office.Interop.MSProject.PjSaveType.pjSave);
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();
2FileStream St = new FileStream("Project.mpp", FileMode.Open);
3Project prj = reader.Read(St);
4St.Close();
5TaskLink taskLink = new TaskLink(prj.RootTask.Children[0] as Aspose.Tasks.Task, prj.RootTask.Children[1] as Aspose.Tasks.Task, TaskLinkType.FinishToStart);
6prj.AddTaskLink(taskLink);
7taskLink = new TaskLink(prj.RootTask.Children[1] as Aspose.Tasks.Task, prj.RootTask.Children[2] as Aspose.Tasks.Task, TaskLinkType.FinishToStart);
8prj.AddTaskLink(taskLink);
9taskLink = new TaskLink(prj.RootTask.Children[2] as Aspose.Tasks.Task, prj.RootTask.Children[3] as Aspose.Tasks.Task, TaskLinkType.FinishToStart);
10prj.AddTaskLink(taskLink);
11taskLink = new TaskLink(prj.RootTask.Children[3] as Aspose.Tasks.Task, prj.RootTask.Children[4] as Aspose.Tasks.Task, TaskLinkType.FinishToStart);
12prj.AddTaskLink(taskLink);
13taskLink = new TaskLink(prj.RootTask.Children[1] as Aspose.Tasks.Task, prj.RootTask.Children[4] as Aspose.Tasks.Task, TaskLinkType.FinishToStart);
14prj.AddTaskLink(taskLink);
15ArrayList allLinks = new ArrayList(prj.TaskLinks);
16foreach (TaskLink taskLink1 in allLinks)
17{
18 Console.WriteLine("From ID = " + taskLink1.PredTask.Id + "=>To ID = " + taskLink1.SuccTask.Id);
19 Console.WriteLine("________________________________________");
20}
21prj.Save("Project1.mpp", Aspose.Tasks.Saving.SaveFileFormat.MPP);