Ajout d'une nouvelle tâche avec Aspose.Tasks vs VSTO
En travaillant avec les fichiers Microsoft Project (MPP / XML), vous devez souvent ajouter de nouvelles tâches aux projets. Cet article montre comment charger des fichiers MPP dans vos applications .NET et ajouter de nouvelles tâches à l’aide d’API VSTO traditionnelles et de la bibliothèque .NET ASPOSE MODERNE.
Aperçu
La capacité de gérer les tâches par programme est essentielle dans les workflows de planification de projet automatisés. Cet article montre comment insérer de nouvelles tâches dans les fichiers de projet Microsoft à l’aide de deux approches:
- Microsoft Office Automation (VSTO): s’appuie sur l’application Microsoft Project installée et COM Interop.
- Aspose.tasks pour .NET: Une bibliothèque .NET autonome qui ne nécessite pas d’installation du projet Microsoft.
Les deux exemples illustrent le même scénario - ajoutant une seule tâche nommée " Task1 "
- pour vous aider à comprendre clairement les différences de syntaxe, de dépendances et de modèle de programmation.
Ajouter une tâche à l’aide de VSTO
Le code suivant montre comment ajouter une nouvelle tâche à un fichier .MPP
existant à l’aide de VSTO (Visual Studio Tools for Office). Cette approche dépend de l’interopt et nécessite l’installation du projet Microsoft et correctement sous licence sur la 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: