vb.net - improving MS Project VB/VBA task creation -
at moment have code creates new tasks, it's buggy , inconsistent.
public sub create_milestones() proj = globals.thisaddin.application.activeproject dim mytask msproject.task application.screenupdating = false each mytask in application.activeselection.tasks application.selecttaskfield(row:=1, column:="name") application.inserttask() application.settaskfield(field:="duration", value:="0") application.settaskfield(field:="start", value:=mytask.finish) application.settaskfield(field:="name", value:=mytask.name & " - milestone") application.settaskfield(field:="resource names", value:=mytask.resourcenames) application.settaskfield(field:="text3", value:="milestone") application.ganttbarformat(ganttstyle:=3, startshape:=13, starttype:=0, startcolor:=255, middleshape:=0, middlepattern:=0, middlecolor:=255, endshape:=0, endcolor:=255, endtype:=0) application.selecttaskfield(row:=1, column:="name") next application.selecttaskfield(row:=-1, column:="name") application.selectrow(row:=0) application.rowdelete() application.screenupdating = true msgbox("done") end sub
it seems go far when looping through selected tasks , creates 1 task many, worked around going , deleting task doesn't seem best solution me.
i realise bit of code in vb.net can work vba too.
is there better way create , assign values new tasks?
the problem task can solved storing collection (or list in .net) of selected tasks , looping through those. i'm posting solution in vba since relevant other viewers; can post vb.net version if needed.
application.screenupdating = false dim proj project set proj = application.activeproject dim mytask task dim coltasks new collection each mytask in application.activeselection.tasks coltasks.add mytask, cstr(mytask.uniqueid) next mytask dim object each in coltasks set mytask = activeproject.tasks.uniqueid(i) dim newtask task set newtask = activeproject.tasks.add(mytask.name & " - milestone", mytask.id + 1) newtask.duration = 0 newtask.predecessors = mytask.id & "ff" newtask.text3 = "milestone" newtask.resourcenames = mytask.resourcenames application.selectrow newtask.id, false application.ganttbarformat ganttstyle:=3, startshape:=13, starttype:=0, startcolor:=255, middleshape:=0, middlepattern:=0, middlecolor:=255, endshape:=0, endcolor:=255, endtype:=0 next application.selectrow coltasks(1), false application.selecttaskfield row:=0, column:="name" application.screenupdating = true
i changed few things: 1) rather hard-coding start field, use task relationship keep it's task when task moves; 2) since zero-duration tasks have no work, not necessary add resources.
update
here's vb.net version:
dim projapp msproject.application = globals.thisaddin.application projapp.screenupdating = false dim proj msproject.project = projapp.activeproject dim seltasks new list(of msproject.task) each mytask msproject.task in projapp.activeselection.tasks seltasks.add(mytask) next mytask each mytask in seltasks dim newtask msproject.task = proj.tasks.add(mytask.name & " - milestone", mytask.id + 1) newtask.duration = 0 newtask.predecessors = mytask.id & "ff" newtask.text3 = "milestone" newtask.resourcenames = mytask.resourcenames projapp.selectrow(newtask.id, false) projapp.ganttbarformat(ganttstyle:=3, startshape:=13, starttype:=0, startcolor:=255, middleshape:=0, middlepattern:=0, middlecolor:=255, endshape:=0, endcolor:=255, endtype:=0) next projapp.selectrow(seltasks(0).id, false) projapp.selecttaskfield(row:=0, column:="name") projapp.screenupdating = true