Get project from task object

A

Albin

Hi,

Maybe a basic question ... if I have a Task object how can I get its
corresponding Project object? The scenario is that in the
Application_ProjectBeforeTaskChange(Task task, PjField field, object newVal,
ref bool cancel) event handler method I get a Task object and I need to
access its corresponding Project object. Using task.Project just returns the
name of the Project. I tried using task.Application.ActiveProject but
sometimes ProjectBeforeTaskChange event is not triggered for a task changed
in the active project, eg, when opening a new file through "File > New..." or
pressing Cntrl+N.

Thanks for any help.
 
B

Bill B

Generally, the Parent object will give the task's project. If you have a
project with subprojects, the subproject that might be a task's parent is
not always loaded. Here's what I do to make sure I get a project object:

Function GetProjectFromTask(t as task) as Project
On Error Resume Next
Set GetProjectFromTask = t.Parent
If Err Or GetProjectFromTask Is Nothing Then ' subproject isn't loaded
Set GetProjectFromTask = Projects(t.Project) ' may load sub, may not
If Err Or GetProjectFromTask Is Nothing Then
If InStr(t.Project, ".mpp") <> 0 Then ' couldn't find with mpp
Set GetProjectFromTask = Projects(Left(t.Project,
InStr(t.Project, ".mpp") - 1)) ' try without
Else ' couldn't find without .mpp
Set GetProjectFromTask = objProj.Projects(t.Project &
".mpp")
Err.Clear
End If
If Err Then
MsgBox "No Luck"
Set GetProjectFromTask = Nothing
End If
End If
End If
On Error GoTo 0
End Function

Bill B
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top