none
Creating a task field list in c# project RRS feed

  • Question

  • Hi All,

    I am writing a small application to simply my monthly forecasting and reporting.  I'm extracting data from my msProject file into a c# application with a local database.  I want the user to be able to select which task data fields to extract into the database at run time.  To achieve this I'm trying to extract from msProject a list of all used fields.

    Following advice from some forum posts I've been able to create a list of fields, based on looping through each Table and taking the TableField names.  The field names are stored as strings.  Enterprise field are string representations of integers.

    I now want to be able to get the data for each task for the fields selected by the user.

    I'm having trouble using "FieldNameToFieldConstant" to GetField value.  I can use it perfectly well in a VBA macro but can find how to use the function in a C# application.  I found a post where someone reporting using...

    task.GetField(msProject.Application.FieldNameToFieldConstant(fieldName,...)

    I can't get to work for me though, when I try msProject.Application. my only available choices are "Equals" and "ReferenceEquals".

    So to sum up, how do I us "FieldNameToFieldConstant" in a c# application or if someone has a better way to create a list of fields that would be great.

    Thanks in advance

    Friday, April 18, 2014 7:44 AM

Answers

  • OK, so I worked it out, answer provided below for anyone else who is interested.

    The answer to my first questions is that FieldNameToFieldConstant is access via an instance of  msProject.Application, in my code below MSP.Application app = new MSP.Application();

    My Code now searches through every table, and for every field in each table creates a custom object which contains the field name and ID number, each new custom field is tested against the contents of the List<clsFieldList> and if its unique its added to the list.  At the end of the process I have a list of all unique fields from all tables in the project file. 

    Hope this is helpful to someone.

    Cheers

            

    using MSP = Microsoft.Office.Interop.MSProject;

    public static List<clsFieldList> FieldsFromTables() { //TODO: move this method to a new thread. //this method searches through each table in the project //gathers a collection of all unique fields in the tables //calling method must ensure that MSProject is open with an project file loaded. List<clsFieldList> fldNames = new List<clsFieldList>(); ; MSP.Application app = new MSP.Application(); MSP.Project proj = app.ActiveProject; MSP.Tables taskTables = proj.TaskTables; int progress = 0; int i = 0; //loop through each table foreach (MSP.Table tskTable in taskTables) { //loop through each field in each table foreach (MSP.TableField tskTableField in tskTable.TableFields) { //prepare to create a new clsFieldList object string title = GetFieldName(tskTableField); long fldID = (long)tskTableField.Field; string fldType = tskTable.GetType().ToString(); //create the new object and check if it exists already in the List clsFieldList newField = new clsFieldList(MSP.PjFieldType.pjTask, fldID, title, fldType); if (!fldNames.Contains(newField)) { //TODO: convert the DataType to string, long etc fldNames.Add(newField); } } } //fldNames now contains a list of clsFieldList which represent the fields used in every table in the active project. return fldNames; } private static string GetFieldName(MSP.TableField tblFld) { //This method is a C# conversion of a VBA example I found on the internet. I'd give credit where its due here but I printed out the code and now can't find it again. MSP.Application app = new MSP.Application(); //find the field name (actually column heading) for a field in a data table long lngFieldID; string strResult = ""; lngFieldID = (long)tblFld.Field; //if the Field Title is not null then set that as the field name. if (tblFld.Title != null) { strResult = tblFld.Title.Trim(); } if(strResult.Length == 0) { //if strResult is still zero length then the field title must have been null, check if its a custom field try { //try to get the custome field name - this will come back blank if its not a custom field strResult = app.CustomFieldGetName((MSP.PjCustomField)lngFieldID).Trim(); } catch { } finally { strResult = app.FieldConstantToFieldName((MSP.PjField)lngFieldID).Trim(); //use the field name } } return strResult; }


    • Marked as answer by SpikeSloan Wednesday, April 23, 2014 12:03 AM
    Wednesday, April 23, 2014 12:02 AM