2010-09-30 107 views
0

我目前正在研究將Microsoft Dynamics CRM產品需求導入Microsoft Project的VBA宏。將Task.Duration分配給VBA中已添加的任務Microsoft Project

我用下面的代碼添加/更新任務:

Function AddTask(strText As String, lngDuration As Long, taskParent As Task) 
    Dim oldTask As Task 
    Set oldTask = taskParent.OutlineChildren(strText) 
    If oldTask Is Nothing Then 
     Dim newTask As Task 
     Set newTask = taskParent.OutlineChildren.Add(Name:=strText, Before:=LastIndexOf(taskParent) + 1) 
     newTask.OutlineLevel = taskParent.OutlineLevel + 1 
     newTask.Duration = lngDuration 
     Set AddTask = newTask 
    Else 
     oldTask.Duration = lngDuration 
     Set AddTask = oldTask 
    End If 
End Function 

這完全適用新的任務,但不幸的是試圖更新舊的任務持續時間屬性,當我得到一個奇怪的錯誤。

Run-Time Error '1101' 

Argument value is not valid 

我真的不明白是什麼

newTask.Duration = lngDuration 

oldTask.Duration = lngDuration 

之間的差別這是怎麼回事?
請幫忙!

回答

0

自己找到了!

問題是有時候任務會被添加到舊任務中。所以它現在是一個包含幾個子任務的父任務。根據定義,持續時間現在是所有兒童持續時間的總和。因此,父任務的持續時間不能手動更改,因爲它自動更新。

因此,一個簡單的檢查,如果舊任務包含任何OutlineChildren,然後跳過它解決了我的問題。

If oldTask.OutlineChildren.Count = 0 Then 
    oldTask.Duration = lngDuration 
End If 

感謝

相關問題