Adding a New Task
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);
This C# code demonstrates how to programmatically open, modify, and save a Microsoft Project file (.mpp) using Microsoft Office Interop libraries. It launches an instance of Microsoft Project, opens an existing file in read-only resource pool mode, and accesses the active project. The code then creates a new task named “Task1” at the top of the task list, sets its start date, assigns a duration of three working days (expressed in minutes), and populates a custom field (Text1). This example showcases how developers can automate task creation and data entry into Microsoft Project files through Interop. While this approach depends on Microsoft Project being installed on the host system and operates only on Windows, it enables tight integration with native application behavior. Aspose.Tasks provides a platform-independent API that allows similar operations—such as reading, editing, and saving project files—without requiring Microsoft Project.
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);
This code sample demonstrates how to use Aspose.Tasks for .NET to read, manipulate, and save Microsoft Project data programmatically without requiring Microsoft Project to be installed. The ProjectReader class is used to load an existing .mpp file, and a new task named “Task1” is created and configured with a specific actual start date, a 1-day duration, and a duration unit of Day. The task is then added to the root task’s child collection, effectively inserting it into the project hierarchy. After adding the task, the CalcTaskIds() and CalcTaskUids() methods are called to ensure that task IDs and UIDs are recalculated correctly, maintaining consistency across the task structure. Finally, the modified project is saved to disk in the Microsoft Project XML format using the Save() method.