Hello,
I am trying to create a project with two tasks, for the first task I know what is the start date is, and its duration, for the second task I know its duration but don’t know the start and finish date; however, I want this task to show up starting right after first task finish as show below:
Task1 7/9/2012 duration 1 day
Task2 7/10/2012 duration 1 month
I created this project based on the example provided on your site, but the second task always ends up starting the day the file is open up on the MS Project. It seems like MS scheduling engine think if it doesn’t have start date then it should get the current date.
Any idea?
Hi Kevin,
Following code should work in your scenario.
Project project = new Project();
project.StartDate = new DateTime(2010, 12, 15, 8, 0, 0);
project.FinishDate = new DateTime(2011, 12, 31, 17, 0, 0);
project.MinutesPerDay = 8 * 60;
project.MinutesPerWeek = 5 * 8 * 60;
project.DaysPerMonth = 20;
project.DefaultStartTime = new DateTime(1, 1, 1, 8, 0, 0);
project.DefaultFinishTime = new DateTime(1, 1, 2, 17, 0, 0);
project.DurationFormat = TimeUnitType.Hour;
project.WorkFormat = TimeUnitType.Hour;
Calendar standard = new Calendar("Standard");
standard = Calendar.MakeStandardCalendar(standard);
project.Calendars.Add(standard);
project.Calendar = standard;
//Create root task.
Task root = new Task();
root.Uid = project.NextTaskUid;
project.RootTask = root;
//Add child task
Task task1 = new Task("Task1");
task1.ActualStart = DateTime.Parse("10-Aug-2011 9:00 AM");
//set duration in hours
task1.Duration = new TimeSpan(8, 0, 0);
task1.DurationFormat = TimeUnitType.Day;
root.Children.Add(task1);
//Add child task
Task task2 = new Task("Task2");
task2.ActualStart = standard.GetFinishDateByStartAndWork(task1.ActualStart, task1.Duration).AddDays(1);
//set duration in hours
task2.Duration = new TimeSpan(24, 0, 0);
task2.DurationFormat = TimeUnitType.Day;
root.Children.Add(task2);
project.CalcTaskIds();
project.CalcTaskUids();
project.CalcCalendarUids();
ProjectWriter writer = new ProjectWriter();
writer.Write(project, "Project1.xml", TasksDataFormat.XML);
Please feel free to contact us in case you have further comments or questions.
Best Regards,