Sunday, April 27, 2014

Displaying the column names for MS Project in C#


Whilst, MSDN provides a very updated and informative developer reference for MS Office  suite in general; it seems to have missed out the same for MS Project .

I was recently developing a MS Project addin in C# using VSTO, and came across several challenges ; but I am happy to say I completed the addin in due time.

The following code displays the name of the columns in the currently open project .

   MSProject.Project activeProject  = Globals.ThisAddIn.Application.ActiveProject;
    MSProject.Table prjTable = prjTable.TaskTables[activeProject  .CurrentTable];

    for (int i = 1; i <= prjTable .TableFields.Count; i++)
    {
         MSProject.PjField field = objTable.TableFields[i].Field;
         string colName  = Globals.ThisAddIn.Application.FieldConstantToFieldName(field).ToString();
        MessageBox.Show(colName);

    }
         
    if (activeProject  != null)
    {
        Marshal.ReleaseComObject(activeProject  );
        objProject = null;
    }

    if (prjTable != null)
    {
       Marshal.ReleaseComObject(prjTable );
       prjTable = null;
    }


There are two other points that should be noticed in this post :-

a) Usage of for loop instead of foreach loop. According to OOM.NET: Part 2 - Outlook Item Leaks , foreach  loops causes a memory leak while for does not. I am following this principle for all the Office addins.

b) Usage of Marshal.ReleaseComObject - Its usage is mandatory as all the objects being dealt with are COM objects

.

No comments :