locked
Custom tables in Microsoft Project Add-In RRS feed

  • Question

  • How can I create a custom table in Microsoft Project 2013 by code (C#)?

    I'm trying to use MSProject.TaskTables.Add but cannot find the right way to use it. I want to create a new table and set the columns with new names for custom fields.

    Thanks in advance


    Rene Alvarez

    Tuesday, December 1, 2015 4:25 AM

Answers

  • This is one way to do it.

    //First create a new table (pj is the current project, MSProject.Project)
    var newTable = pj.TaskTables.Add("CustomTableName", MSProject.PjField.pjTaskName, true);
    //Get a custom field
    MSProject.PjField customField = Application.FieldNameToFieldConstant("CustomFieldName", MSProject.PjFieldType.pjTask);
    //Add custom field to your custom table
    newTable.TableFields.Add(customField, MSProject.PjAlignment.pjLeft, 10, "CustomTitle", MSProject.PjAlignment.pjCenter);


    Tuesday, December 1, 2015 10:39 AM

All replies

  • This is one way to do it.

    //First create a new table (pj is the current project, MSProject.Project)
    var newTable = pj.TaskTables.Add("CustomTableName", MSProject.PjField.pjTaskName, true);
    //Get a custom field
    MSProject.PjField customField = Application.FieldNameToFieldConstant("CustomFieldName", MSProject.PjFieldType.pjTask);
    //Add custom field to your custom table
    newTable.TableFields.Add(customField, MSProject.PjAlignment.pjLeft, 10, "CustomTitle", MSProject.PjAlignment.pjCenter);


    Tuesday, December 1, 2015 10:39 AM
  • Thanks Henrik,

    I'm getting this error on the last parameter of the second line (MSProject.PjFieldType.pjTask)

    An exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll but was not handled in user code.

    The two things I changed in the code you shared is the object for Application (it's app.Application in my case) and the object for the project which is project instead of pj.


    Rene Alvarez

    Tuesday, December 1, 2015 3:40 PM
  • Three things to check:

    • Make sure the custom field does exist
    • Make sure you write the exact same name of the custom field
    • Make sure that the task type you use in the second parameter is the same as the type of the existing custom field
    Tuesday, December 1, 2015 4:07 PM
  • Thanks! That was it. I was using the code with a wrong custom field name.


    Rene Alvarez

    Tuesday, December 1, 2015 5:55 PM