Adding a New Task
Contents
[
Hide
Show
]Code Examples
VSTO
To add 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 add a new task.
1//Create an Application object
2
3Microsoft.Office.Interop.MSProject.Application projectApplication = new MSProject.Application();
4
5object missingValue = System.Reflection.Missing.Value;
6
7projectApplication.FileOpenEx("Project2.mpp",
8
9 missingValue, missingValue, missingValue, missingValue,
10
11 missingValue, missingValue, missingValue, missingValue,
12
13 missingValue, missingValue, PjPoolOpen.pjPoolReadOnly,
14
15 missingValue, missingValue, missingValue, missingValue,
16
17 missingValue);
18
19Microsoft.Office.Interop.MSProject.Project project = projectApplication.ActiveProject;
20
21Microsoft.Office.Interop.MSProject.Task task;
22
23task = project.Tasks.Add("Task1", 1);
24
25task.Start = "8/23/2012";
26
27task.Duration = 3 * 8 * 60;
28
29task.Text1 = "Task1";
30
31projectApplication.FileCloseAll(Microsoft.Office.Interop.MSProject.PjSaveType.pjSave);
Aspose.Tasks
To add tasks to project files 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 add a new task.
1ProjectReader reader = new ProjectReader();
2
3Project project = reader.Read("Project.mpp");
4
5Aspose.Tasks.Task task = new Aspose.Tasks.Task("Task1");
6
7task.ActualStart = DateTime.Parse("23-Aug-2012");
8
9task.Duration = new TimeSpan(24, 0, 0);
10
11task.DurationFormat = TimeUnitType.Day;
12
13project.RootTask.Children.Add(task);
14
15project.CalcTaskIds();
16
17project.CalcTaskUids();
18
19project.Save("OutputProject.xml", Aspose.Tasks.Saving.SaveFileFormat.XML);