Background Information
Using the TimeScaleData
function in a C# or VB.NET loop (when iterating through a list), may result in a
COMException error. This error is caused by an
internal resource limit in Microsoft Project, and because the .NET garbage
collector does not clear up the references fast enough.
Resolution
The solution is to call the
System.GC.Collect() at the end of the
loop. The following is an example that iterates through the assignments of a
resource (and does nothing....)
Private Sub transferResData(ByRef
res As MSProject.Resource,
ByVal startDate As Date, _
ByVal endDate As Date)
Dim
assign As MSProject.Assignment
Dim
tsv As MSProject.TimeScaleValues
Dim
value As MSProject.TimeScaleValue
Try
If res.Assignments.Count > 0 Then
For Each assign In res.Assignments
If assign.TaskUniqueID > 0 Then
tsv = assign.TimeScaleData(startDate,
endDate, _
MSProject.PjAssignmentTimescaledData.pjAssignmentTimescaledWork, _
MSProject.PjTimescaleUnit.pjTimescaleMonths, 1)
For Each value In tsv
Try
' Do something with the value
Catch ex As Exception
MsgBox(ex.Message)
End Try
System.GC.Collect()
Next value
End If
Next assign
End If
Catch ex As Exception
MsgBox(ex.Message)
End
Try
End Sub
Contributed by Project Server Expert, Hugues Perron
With thanks to Brook Miles for the solution