none
MsProject How to insert the task in MPP ? using vb.net or c# RRS feed

  • Question

  • I have created an Office Add In to maintain the Tasks in the Ms Project file where i would like to insert the task instead of appending at the end of the row.

    Could you please let me know how to insert the task in between the several tasks in the specific position(row).

    Thank you for your support in advance.


    SomaSundaram R

    Thursday, September 8, 2016 11:24 AM

Answers

  • >>>I would like to iterate through the task names and if I found what I am searching for then I would like to include the task new task before that task.

    According to your description, you could use Tasks.Add method to add a Task object before that task, refer to below VBA code:

    Sub Demo()
       
       Dim name As String
       name = "Readiness"
       
       Dim curTask As Task
       
       For i = 1 To ActiveProject.Tasks.Count
         If ActiveProject.Tasks(i).name = name Then
            Set curTask = ActiveProject.Tasks.Add("Readiness2", i)
            Exit For
         End If
       Next
       
    End Sub

    The result:

    For more information, click here to refer about Tasks.Add Method (Project)

    • Edited by David_JunFeng Friday, September 16, 2016 7:21 AM
    • Proposed as answer by David_JunFeng Sunday, September 18, 2016 2:05 PM
    • Marked as answer by David_JunFeng Monday, September 19, 2016 5:36 AM
    Friday, September 16, 2016 7:21 AM

All replies

  • >>>Could you please let me know how to insert the task in between the several tasks in the specific position(row).

    According to your description, I suggest that you could use "Record Macro" to get VBA code when you insert a task manually, then you could convert VBA code into VB.Net or C#:

    1.Click the File tab.
    2.Click Options.
    3.Click Customize Ribbon.
    4.Under Customize the Ribbon and under Main Tabs, select the Developer check box.
    5.Click Record Macro

    You will get VBA code like below:

    Sub Macro1()
    
        SelectRow Row:=-9
        InsertTask
        SetTaskField Field:="Name", Value:="222"
        SelectTaskField Row:=0, Column:="Duration"
        SetTaskField Field:="Duration", Value:="1 day"
        SelectTaskField Row:=0, Column:="Start"
        SetTaskField Field:="Start", Value:="Tue 9/6/16"
        SelectTaskField Row:=8, Column:="Finish"
        
    End Sub

    Thanks for your understanding.
    Friday, September 9, 2016 5:36 AM
  • I would like to iterate through the task names and if I found what I am searching for then I would like to include the task new task before that task.

    Small code:

    For Each tsk As Microsoft.Office.Interop.MSProject.Task In project.Tasks If (tsk.Name = myTaskNameSearch) Then 'find the index of this task and create the new task before this ' I need a help to insert the task here End If

    Next



    SomaSundaram R

    Tuesday, September 13, 2016 10:00 AM
  • >>>I would like to iterate through the task names and if I found what I am searching for then I would like to include the task new task before that task.

    According to your description, you could use Tasks.Add method to add a Task object before that task, refer to below VBA code:

    Sub Demo()
       
       Dim name As String
       name = "Readiness"
       
       Dim curTask As Task
       
       For i = 1 To ActiveProject.Tasks.Count
         If ActiveProject.Tasks(i).name = name Then
            Set curTask = ActiveProject.Tasks.Add("Readiness2", i)
            Exit For
         End If
       Next
       
    End Sub

    The result:

    For more information, click here to refer about Tasks.Add Method (Project)

    • Edited by David_JunFeng Friday, September 16, 2016 7:21 AM
    • Proposed as answer by David_JunFeng Sunday, September 18, 2016 2:05 PM
    • Marked as answer by David_JunFeng Monday, September 19, 2016 5:36 AM
    Friday, September 16, 2016 7:21 AM