none
Add a List to Project task C#

    Pertanyaan

  • Looking for a way to add a List<> to Project task in C#?

    Want to add on a blank row name, duration, text8...

    I can use activeProject.Tasks.Add(Name: "string"); but can't figure how to add duration from my List<>.  I have read and Excel doc and want to read a number of matching columns back in to Project but can't figure out the Add method only populates the Name column...

    Tried create a task but didn't work. If I uncomment the activeProject.Tasks.Add() just populates the name.

                        MSProject.Task task;
                        task.Text8 = "test";
                        //activeProject.Tasks.Add(Name: xlRowList[index].area);
                        //activeProject.Tasks.Add(Name: xlRowList[index].craft);
                        
     
                        //activeProject.Tasks.Add(Name: xlRowList[index].woNum, Before: taskCount);
                        //activeProject.Tasks.Add(Name: xlRowList[index].taskDescription, Before: taskCount);
                        //activeProject.Tasks.Add(Name: xlRowList[index].equipNum, Before: taskCount);
                        //activeProject.Tasks.Add(Name: xlRowList[index].equipName, Before: taskCount);
                        //activeProject.Tasks.Add(Name: xlRowList[index].duration, Before: taskCount);
                        //activeProject.Tasks.Add(Name: xlRowList[index].resource, Before: taskCount);
                        //activeProject.Tasks.Add(Name: xlRowList[index].start, Before: taskCount);
                        //activeProject.Tasks.Add(Name: xlRowList[index].finish, Before: taskCount);



    Jumat, 18 Mei 2018 12.50

Jawaban

  • Chris,

    Have you looked here?  Walkthrough: Creating Your First VSTO Add-in for Project

    The link includes a c# snippet to create a project then add and populate a task using an add-in.  You should be able to adapt it for your use.  (For a one-off, i.e. custom use, I would use vba.) 

    Note: ActiveProject is a member of the Microsoft.Office.Interop.MSProject.Application object.  My vb.net add-ins initiate it as projApp = Globals.ThisAddIn.Application.  Then ActiveProject can be browsed as a member of projApp throughout the VSTO project.  (Microsoft.Office.Interop.MSProject.dll has to be included in the project references first.)

    Good luck, tom


    • Diedit oleh Tom BoyleBPC Jumat, 18 Mei 2018 22.23 Added note on ActiveProject
    • Ditandai sebagai Jawaban oleh mainepaine Sabtu, 19 Mei 2018 11.48
    Jumat, 18 Mei 2018 22.12

Semua Balasan

  • Chris,

    The Tasks.Add Method only creates a new task with a task name. There are several ways to add other task properties such as duration, start, predecessors, notes, etc. One method is to access the appropriate field directly with something like:

    ActiveProject.Tasks(ID).Duration = 10

    Another is to use the Task.SetField Method

    Hope this helps.

    John

    Jumat, 18 Mei 2018 13.30
  • I am looking for C# and ActiveProject is not in the object browser...

    Jumat, 18 Mei 2018 14.18
  • Chris,

    I don't work with C#. The syntax I gave you is for VBA, which tends to run faster since it's resident in the application.

    John

    Jumat, 18 Mei 2018 15.55
  • Chris,

    Have you looked here?  Walkthrough: Creating Your First VSTO Add-in for Project

    The link includes a c# snippet to create a project then add and populate a task using an add-in.  You should be able to adapt it for your use.  (For a one-off, i.e. custom use, I would use vba.) 

    Note: ActiveProject is a member of the Microsoft.Office.Interop.MSProject.Application object.  My vb.net add-ins initiate it as projApp = Globals.ThisAddIn.Application.  Then ActiveProject can be browsed as a member of projApp throughout the VSTO project.  (Microsoft.Office.Interop.MSProject.dll has to be included in the project references first.)

    Good luck, tom


    • Diedit oleh Tom BoyleBPC Jumat, 18 Mei 2018 22.23 Added note on ActiveProject
    • Ditandai sebagai Jawaban oleh mainepaine Sabtu, 19 Mei 2018 11.48
    Jumat, 18 Mei 2018 22.12
  •                    After a few modifications...

                        Task newTask = activeProject.Tasks.Add(xlRowList[index].taskDescription);
                        newTask.Text8 = xlRowList[index].area;
                        newTask.Text9 = xlRowList[index].craft;
                        newTask.Text3 = xlRowList[index].woNum;   
                        newTask.Text5 = xlRowList[index].equipNum;
                        newTask.Text6 = xlRowList[index].equipName;
                        newTask.Duration = xlRowList[index].duration.ToString();
                        newTask.ResourceNames = xlRowList[index].resource;
                        newTask.Start = DateTime.Now;
                        //newTask.Finish = xlRowList[index].finish;

    You are AWESOME!!!

    Sabtu, 19 Mei 2018 11.49
  • Glad to help, and thanks for the feedback.
    Sabtu, 19 Mei 2018 16.22