Adding a New Task with Aspose.Tasks vs VSTO

While working with Microsoft Project (MPP/XML) files, you often need to add new tasks to projects. This article shows how to load MPP files in your .NET applications and add new tasks to your project using VSTO and Aspose.Tasks for .NET.

Add a Task Using VSTO

To add a task using VSTO:

  1. Create a new project in Visual Studio.
  2. In the Solution Explorer, right-click and select Add Reference.
  3. Select the COM components tab, and select Microsoft Project 12.0 Object Library.
  4. 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.

 1Microsoft.Office.Interop.MSProject.Application projectApplication = new Application();
 2object missingValue = System.Reflection.Missing.Value;
 3projectApplication.FileOpenEx(@"C:\Project1.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;
10Microsoft.Office.Interop.MSProject.Task task;
11task = project.Tasks.Add("Task1", 1);
12task.Start = "8/23/2012";
13task.Duration = 3 * 8 * 60;
14task.Text1 = "Task1";
15projectApplication.FileCloseAll(Microsoft.Office.Interop.MSProject.PjSaveType.pjSave);

Add a Task Using Aspose.Tasks for .NET

To add tasks to project files using Aspose.Tasks for .NET:

  1. Create a new project in Visual Studio.
  2. In the Solution Explorer, right-click and select Add Reference.
  3. Select .NET tab and select Aspose.Tasks.
  4. 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.

 1Project project = new Project("New Project.mpp");
 2
 3Task task = project.RootTask.Children.Add("Task1");
 4task.Set(Tsk.ActualStart, DateTime.Parse("23-Aug-2012"));
 5
 6// Set duration in hours
 7task.Set(Tsk.Duration, project.GetDuration(24, TimeUnitType.Hour));
 8task.Set(Tsk.DurationFormat, TimeUnitType.Day);
 9
10project.Save("AddNewTask_out.xml", SaveFileFormat.XML);

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.