Dinesh Sabnani's Blog

This blog is intended to help people ramping up with Microsoft technologies.

API for Calendars in Project Professional 2010

API for Calendars in Project Professional 2010

Rate This
  • Comments 1

Continuing with my article on Calendars on Project Professional 2010, I would like to share knowledge on API for calendars in project professional 2010.

Following are the topics covered in this article:

  • Introduction to Interfaces for calendars in project professional 2010.
  • Introduction to Enums used for exceptions in calendars
  • Code to read current selected Task
  • Code to read resources of the Task
  • Code to read Project calendar
  • Code to read Task calendar
  • Code to read Resource calendar
  • Code to read default workweek
  • Code to read all workweeks other than default
  • Code to read shifts in workweek
  • Code to read exceptions in calendar

Introduction to Interfaces for calendars in project professional 2010

Following are the interfaces present in “Microsoft.Office.Interop.MSProject” dll which are used to read the calendar information in the project. Please make a note I will be using “MSProject” to refer this dll in this blog.

MSProject.Task

It’s an Interface used to refer Task in the project

MSProject.Project

It’s an Interface used to refer Project

MSProject.Resource

It’s an Interface used to refer Resource in the project

MSProject.Calendar

It’s an Interface used to refer Calendar in the project

MSProject.WorkWeek

It’s an Interface used to get work weeks present in the calendar other than default work week.

MSProject.WeekDay

It’s an Interface used to refer to days in Default Work week in the Calendar

MSProject.WorkWeekDay

It’s an Interface used to refer to days in work week present in the calendar other than default work week.

MSProject.Shift

It’s an Interface used to refer shift in the work week in the calendar

MSProject.Exception

It’s an Interface used to refer exception in the calendar

 

Introduction to Enums used for exceptions in calendars

Following are the Enums present in “Microsoft.Office.Interop.MSProject” dll which are used to read the calendar information in the project.

MSProject.PjExceptionType

It’s an enum to refer type of exception.

Following are the enum values:

pjDaily = 1

pjYearlyMonthDay = 2

pjYearlyPositional = 3

pjMonthlyMonthDay = 4

pjMonthlyPositional = 5

pjWeekly = 6

pjDayCount = 7

 

MSProject.PjExceptionPosition

It’s en enum to refer to exception’s position.

Following are the enum values:

pjFirst = 0

pjSecond = 1

pjThird = 2

pjFourth = 3

pjLast = 4

 

For example, to create working monthly exception as every first Monday of the month.

MSProject.PjExceptionItem

It’s enum to refer to exception’s Day.

Following are the enum values:

pjItemSunday = 3

pjItemMonday = 4

pjItemTuesday = 5

pjItemWednesday = 6

pjItemThursday = 7

pjItemFriday = 8     

pjItemSaturday = 9

 

For example, to create working weekly exception as every Sunday exception.

MSProject.PjMonth

It’s an enum to refer to exception’s month

Following are the enum values:

pjJanuary = 1,

pjFebruary = 2,

pjMarch = 3,

pjApril = 4,

pjMay = 5,

pjJune = 6,

pjJuly = 7,

pjAugust = 8,

pjSeptember = 9,

pjOctober = 10,

pjNovember = 11,

pjDecember = 12,

 

For example, to create working monthly exception as every 15th of January exception.


Code to read current selected Task:

MSProject.Application ApplicationObj = this.Application;

if (ApplicationObj.ActiveSelection.Tasks != null)
{
if (ApplicationObj.ActiveSelection.Tasks.Count > 0)
{
List<MSProject.Task> tasklist = new List<MSProject.Task>();

foreach (MSProject.Task task in ApplicationObj.ActiveSelection.Tasks)
{
tasklist.Add(task);
}
}
}

Code to read resources of the Task:

MSProject.Application ApplicationObj = this.Application;

if (ApplicationObj.ActiveSelection.Tasks != null)
{
if (ApplicationObj.ActiveSelection.Tasks.Count > 0)
{
List<MSProject.Task> tasklist = new List<MSProject.Task>();

foreach (MSProject.Task task in ApplicationObj.ActiveSelection.Tasks)
{
foreach (MSProject.Assignment assignment in task.Assignments)
{
List<MSProject.Resource> resources = new List<MSProject.Resource>();
resources.Add(assignment.Resource);
}
}
}
}

Code to read Project calendar:

MSProject.Application ApplicationObj = this.Application;
MSProject.Calendar projectCalendar = ApplicationObj.ActiveProject.Calendar;

Code to read Task calendar:

MSProject.Application ApplicationObj = this.Application;

if (ApplicationObj.ActiveSelection.Tasks != null)
{
if (ApplicationObj.ActiveSelection.Tasks.Count > 0)
{
List<MSProject.Task> tasklist = new List<MSProject.Task>();

foreach (MSProject.Task task in ApplicationObj.ActiveSelection.Tasks)
{
MSProject.Calendar taskCalendar = task.CalendarObject;
}
}
}

Code to read Resource calendar:

MSProject.Application ApplicationObj = this.Application;

if (ApplicationObj.ActiveSelection.Tasks != null)
{
if (ApplicationObj.ActiveSelection.Tasks.Count > 0)
{
List<MSProject.Task> tasklist = new List<MSProject.Task>();

foreach (MSProject.Task task in ApplicationObj.ActiveSelection.Tasks)
{
foreach (MSProject.Assignment assignment in task.Assignments)
{
MSProject.Calendar resourceCalendar = assignment.Resource.Calendar;
}
}
}
}

Code to read default workweek:

MSProject.Calendar taskCalendar = task.CalendarObject;

foreach (MSProject.WeekDay WeekDay in taskCalendar.WeekDays)
{

}

Code to read all workweeks other than default:

MSProject.Calendar taskCalendar = task.CalendarObject;

foreach (MSProject.WorkWeek comWorkWeek in taskCalendar.WorkWeeks)
{

DateTime startDate = Convert.ToDateTime(comWorkWeek.Start, CultureInfo.InvariantCulture);

DateTime finishDate = Convert.ToDateTime(comWorkWeek.Finish, CultureInfo.InvariantCulture);

foreach (MSProject.WorkWeekDay comWD in comWorkWeek.WeekDays)
{
}
}

Code to read shifts in workweek:

MSProject.Calendar taskCalendar = task.CalendarObject;

foreach (MSProject.WeekDay comWeekDay in taskCalendar.WeekDays)
{
MSProject.Shift shift1 = comWeekDay.Shift1;
MSProject.Shift shift2 = comWeekDay.Shift2;
MSProject.Shift shift3 = comWeekDay.Shift3;
MSProject.Shift shift4 = comWeekDay.Shift4;
MSProject.Shift shift5 = comWeekDay.Shift5;
}

Code to read exceptions in calendar:

foreach (MSProject.Exception comCalendarException in comCalendar.Exceptions)
{
string name = comCalendarException.Name;

DateTime startDate = Convert.ToDateTime(comCalendarException.Start, CultureInfo.InvariantCulture);
DateTime finishDate = Convert.ToDateTime(comCalendarException.Finish, CultureInfo.InvariantCulture);

MSProject.Shift shift1 = comCalendarException.Shift1;
MSProject.Shift shift2 = comCalendarException.Shift2;
MSProject.Shift shift3 = comCalendarException.Shift3;
MSProject.Shift shift4 = comCalendarException.Shift4;
MSProject.Shift shift5 = comCalendarException.Shift5;

int Occurrences = comCalendarException.Occurrences;

}

This was all about using API for calendars in Project Professional 2010, I hope this blog is useful.

                

Comments
  • As I went through creating a Project Professional Add in, I required to go through the calendar provided

Page 1 of 1 (1 items)
Leave a Comment
  • Please add 8 and 2 and type the answer here:
  • Post