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 using traditional VSTO interop APIs and the modern Aspose.Tasks for .NET library.
Overview
The ability to programmatically manage tasks is essential in automated project planning workflows. This article demonstrates how to insert new tasks into Microsoft Project files using two approaches:
- Microsoft Office Automation (VSTO): relies on the installed Microsoft Project application and COM interop.
- Aspose.Tasks for .NET: a standalone .NET library that does not require Microsoft Project to be installed.
Both examples illustrate the same scenario — adding a single task named "Task1"
— to help you clearly understand the differences in syntax, dependencies, and programming model.
Add a Task Using VSTO
The following code demonstrates how to add a new task to an existing .mpp
file using VSTO (Visual Studio Tools for Office). This approach depends on COM interop and requires Microsoft Project to be installed and properly licensed on the machine.
1Microsoft.Office.Interop.MSProject.Application projectApplication = new Application();
2
3object missingValue = System.Reflection.Missing.Value;
4
5projectApplication.FileOpenEx(@"C:\Project1.mpp",
6 missingValue, missingValue, missingValue, missingValue,
7 missingValue, missingValue, missingValue, missingValue,
8 missingValue, missingValue, PjPoolOpen.pjPoolReadOnly,
9 missingValue, missingValue, missingValue, missingValue,
10 missingValue);
11
12Microsoft.Office.Interop.MSProject.Project project = projectApplication.ActiveProject;
13
14Microsoft.Office.Interop.MSProject.Task task;
15task = project.Tasks.Add("Task1", 1);
16
17task.Start = "8/23/2012";
18task.Duration = 3 * 8 * 60; // Duration in minutes (3 working days * 8 hours/day * 60 minutes)
19task.Text1 = "Task1";
20
21projectApplication.FileCloseAll(Microsoft.Office.Interop.MSProject.PjSaveType.pjSave);
Notes
- Duration must be specified in minutes, which is less intuitive compared to using time units directly.
- The API requires careful handling of optional parameters (
Missing.Value
) and COM resource cleanup. - The project file must be opened via
FileOpenEx
and explicitly saved and closed usingFileCloseAll
.
Add a Task Using Aspose.Tasks for .NET
In contrast, the code below shows how to accomplish the same task using Aspose.Tasks for .NET. This API works independently of Microsoft Project and is ideal for web applications, services, and containerized environments.
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);
Highlights
- Tasks are created using a clean object model under
RootTask.Children
. - Duration can be specified in hours, days, or other units using
TimeUnitType
. - The
Set
method provides type-safe, field-specific updates to task properties. - No COM interop or Office installation is required — the code runs cross-platform.
Summary
Aspect | VSTO / Interop | Aspose.Tasks for .NET |
---|---|---|
Requires MS Project | ✅ Yes | ❌ No |
Dependency Type | COM (Microsoft.Office.Interop.MSProject) | Pure .NET |
Cross-platform support | ❌ Windows-only | ✅ Windows, Linux, macOS (via .NET) |
Deployment suitability | ⚠ Desktop/server only | ✅ Web, containers, serverless |
Code complexity | ⚠ High (verbose, boilerplate-heavy) | ✅ Simple and expressive |
Aspose.Tasks enables modern, scalable, and maintainable solutions for working with Microsoft Project formats. Migrating from VSTO unlocks better deployment flexibility, improved reliability, and a more developer-friendly API surface.
To continue exploring, check out the following pages: