Agregar una nueva tarea con Aspose.Tasks vs vsto

Mientras trabaja con archivos de Microsoft Project (MPP/XML), a menudo necesita agregar nuevas tareas a los proyectos. Este artículo muestra cómo cargar archivos MPP en sus aplicaciones .NET y agregar nuevas tareas a su proyecto utilizando VSTO y Aspose.Tasks para .NET.

Agregue una tarea usando vsto

Para agregar una tarea usando vsto:

  1. Crea un nuevo proyecto en Visual Studio.

  2. En el explorador de soluciones, haga clic derecho y seleccione Agregar referencia.

  3. Seleccione la pestaña Componentes COM y seleccione Biblioteca de objetos del Proyecto Microsoft 12.0.

  4. Haga clic en Aceptar.

Esto importa el espacio de nombres de Microsoft.Office.interop.MsProject al comienzo de su código. Use el código del siguiente ejemplo para agregar una nueva tarea.

 1Microsoft.Office.inTerop.MsProject.Application ProjectApplication = new Application ();
 2
 3Objeto MissingValue = System.Reflection.Missing.Value;
 4
 5ProjectApplication.FileOpenex (@"C: \ Project1.mpp",
 6    missingValue, missingValue, missingValue, missingValue,
 7
 8    missingValue, missingValue, missingValue, missingValue,
 9
10
11
12    missingValue, missingValue, missingValue, missingValue,
13
14    missingValue);
15
16Microsoft.Office.Interop.MSProject.Project project = projectApplication.ActiveProject;
17
18Microsoft.Office.Interop.MSProject.Task task;
19
20task = project.Tasks.Add("Task1", 1);
21
22task.Start = "8/23/2012";
23
24task.Duration = 3 * 8 * 60;
25
26task.Text1 = "Task1";
27
28projectApplication.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.