Adding a New Task in VSTO and Aspose.Tasks
Contents
[
Hide
Show
]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
2Microsoft.Office.Interop.MSProject.Application projectApplication = new MSProject.Application();
3object missingValue = System.Reflection.Missing.Value;
4projectApplication.FileOpenEx("Project2.mpp",
5 missingValue, missingValue, missingValue, missingValue,
6 missingValue, missingValue, missingValue, missingValue,
7 missingValue, missingValue, PjPoolOpen.pjPoolReadOnly,
8 missingValue, missingValue, missingValue, missingValue,
9 missingValue);
10Microsoft.Office.Interop.MSProject.Project project = projectApplication.ActiveProject;
11Microsoft.Office.Interop.MSProject.Task task;
12task = project.Tasks.Add("Task1", 1);
13task.Start = "8/23/2012";
14task.Duration = 3 * 8 * 60;
15task.Text1 = "Task1";
16projectApplication.FileCloseAll(Microsoft.Office.Interop.MSProject.PjSaveType.pjSave);
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();
2Project project = reader.Read("Project.mpp");
3Aspose.Tasks.Task task = new Aspose.Tasks.Task("Task1");
4task.ActualStart = DateTime.Parse("23-Aug-2012");
5task.Duration = new TimeSpan(24, 0, 0);
6task.DurationFormat = TimeUnitType.Day;
7project.RootTask.Children.Add(task);
8project.CalcTaskIds();
9project.CalcTaskUids();
10project.Save("OutputProject.xml", Aspose.Tasks.Saving.SaveFileFormat.XML);