Changing a value at a specific taskID within MS Project through C#(Microsoft.Office.Interop.MSProject)

StackOverflow https://stackoverflow.com/questions/16178073

  •  11-04-2022
  •  | 
  •  

Frage

I am trying to edit a task in MS Project. I want to update specific tasks(multiple tasks) at the column "StartDate" with a single date from a dateTimePicker. I am using Microsoft.Office.Interop.MSProject but I cant seem to find the right method, the ActiveProject.Tasks.Add() method doesn't update the tasks, it just adds another task at the specified taskID. I have also tried the replace method... Here is what i have tried:

        public static void editTask(string taskName, DateTime startDate, int taskID)
        {
            ApplicationClass msProj = new ApplicationClass();
            msProj.Visible = true;
            Task addTask = msProj.ActiveProject.Tasks.Add(taskName,taskID);
            addTask.Start = startDate;
        }

        private void button5_Click(object sender, EventArgs e) //Writes to date where names are selected.
        {
                    for (int x = 0; x <= checkedListBox1.CheckedItems.Count - 1; x++)
                    {
                        foreach (var item in checkedListBox1.SelectedItems)
                        {
                            msProj.Replace(Name,Type.Missing,checkedListBox1.SelectedItem,dateTimePicker1.Text,true,true,false,PjField.pjResourceName,PjComparison.pjCompareEquals);
                            //editTask(checkedListBox1.CheckedItems[x].ToString(), DateTime.Parse(dateTimePicker1.Text));
                        }
                    }
        }

I am using VS 2010 and Office Project 2010

If you need more info on how or what I want to do, please ask me. And please excuse my code above, its been edited and changed so much, these Interop things wrinkle my brain...

I couldn't find too much info on Office.Interop for MSProject. Please, any links or help will be appreciated.

Regards,

War es hilfreich?

Lösung

This is how the problem was solved:

private void button5_Click(object sender, EventArgs e) //Writes to date where names are selected.
    {
        ApplicationClass msProj = new ApplicationClass();
        int i = 0;
        foreach (Task t in msProj.ActiveProject.Tasks)
        {
            if (i < checkedListBox1.CheckedItems.Count)
            {
                if (t.ID == int.Parse(checkedListBox1.CheckedItems[i].ToString()))
                {
                    t.Start = DateTime.Parse(dateTimePicker1.Text);
                    i++;
                }
            }
        }
    }

Andere Tipps

You can use:

Task yourTask = project.Tasks.get_UniqueID(UniqueId)

It will return the object of the task, which has a given UniqueId. So it is a method to look for a task with a specific UniqueId. UniqueId of type int. Now you can change the data in the task.

yourTask.Start = DateTime.Parse(dateTimePicker1.Text)

For editing a task, you can have a loop in task(s) and change when ever you want. But be sure about the formats. As an example, task.name is string, most of them are object, so you must convert them. used this way:

            ApplicationClass projectApp = new ApplicationClass();                
            projectApp.FileOpen("C:\\test.mpp", false, Missing.Value, Missing.Value,  Missing.Value,Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,PjPoolOpen.pjPoolReadWrite, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

            // Get the active project
            Project proj = projectApp.ActiveProject;
            foreach (Task task in proj.Tasks)
                {
                                try
                                {
                                    Object per = "25";//any data you want to change
                                    task.PhysicalPercentComplete = per;
                                }
                                catch
                                {

                                }
                }//foreach

At the end you should close ever thing and save them:

            projectApp.FileSaveAs("c:\\sample.mpp", PjFileFormat.pjMPP, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            projectApp.FileCloseAll(PjSaveType.pjSave);

if your editions on tasks are pretty large, it will be worth to create a dictionary like:tasksDictionary<id, counter> at the beginning of your program, and then in every edition, simply call something like this:project.Tasks[tasksDictionary[id]].field = newValue.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top