Discussion:
MS Project COM Exception
(too old to reply)
Jonathan Fong
2003-10-21 00:54:04 UTC
Permalink
Hi,

I have some trouble with MS Project COM object. The following C# code
segment has given me an "An unexpected error occurred with the method"
exception during accessing timeScaleValue.Value after 10 looping in the
foreach. However, there is not problem accessing other properties such as
timeScaleValue.StartDate and timeScaleValue.EndDate in the loop
individually. It seems the MS Project COM object having problem to return
the result of the value property from 11th item in the timeScaleValues
collection.

Resources resources = this._oMSApp.ActiveProject.Resources;
foreach(Resource resource in resources)
{
MSProject.TimeScaleValues timeScaleValues =
resource.TimeScaleData("1/12/1999", "1/12/2003",
MSProject.PjResourceTimescaledData.pjResourceTimescaledCost,
MSProject.PjTimescaleUnit.pjTimescaleDays, 1);

foreach(TimeScaleValue timeScaleValue in timeScaleValues)
{
object val = timeScaleValue.Value; // Exception here
string str = val.ToString();
}
}

I also tested a similar code segment in the VBA. It is working fine if I did
not do any watch mode. Moreover, it had given the same exception result in
the Watches of the debug mode.

Private Sub CommandButton1_Click()
Dim TSV As TimeScaleValues, HowMany As Long
Dim HoursPerDay As String
Set TSV = ActiveCell.Resource.TimeScaleData("1/11/99", "10/11/00",
TimescaleUnit:=pjTimescaleDays)
For HowMany = 1 To TSV.Count
HoursPerDay = HoursPerDay & TSV(HowMany).Value
Next HowMany
MsgBox HoursPerDay
End Sub

Does anyone have faced the similar problem or can someone explain to me what
is the cause of the"An unexpected error occurred with the method"
exception?

Regards
Jonathan
Rod Gill
2003-10-22 04:24:12 UTC
Permalink
Hi,

If the resources workload finishes before your finish date, the value (at
least in VBA returns "" not 0 because it doesn't exist. A zero would suggest
simply no work for that period. The "" signifies the resource has finished,
or hasn't started in this project yet.
--
Rod Gill
Project MVP
For Microsoft Project companion projects, best practices and Project VBA
development services
visit www.projectlearning.com/
Post by Jonathan Fong
Hi,
I have some trouble with MS Project COM object. The following C# code
segment has given me an "An unexpected error occurred with the method"
exception during accessing timeScaleValue.Value after 10 looping in the
foreach. However, there is not problem accessing other properties such as
timeScaleValue.StartDate and timeScaleValue.EndDate in the loop
individually. It seems the MS Project COM object having problem to return
the result of the value property from 11th item in the timeScaleValues
collection.
Resources resources = this._oMSApp.ActiveProject.Resources;
foreach(Resource resource in resources)
{
MSProject.TimeScaleValues timeScaleValues =
resource.TimeScaleData("1/12/1999", "1/12/2003",
MSProject.PjResourceTimescaledData.pjResourceTimescaledCost,
MSProject.PjTimescaleUnit.pjTimescaleDays, 1);
foreach(TimeScaleValue timeScaleValue in timeScaleValues)
{
object val = timeScaleValue.Value; // Exception here
string str = val.ToString();
}
}
I also tested a similar code segment in the VBA. It is working fine if I did
not do any watch mode. Moreover, it had given the same exception result in
the Watches of the debug mode.
Private Sub CommandButton1_Click()
Dim TSV As TimeScaleValues, HowMany As Long
Dim HoursPerDay As String
Set TSV = ActiveCell.Resource.TimeScaleData("1/11/99", "10/11/00",
TimescaleUnit:=pjTimescaleDays)
For HowMany = 1 To TSV.Count
HoursPerDay = HoursPerDay & TSV(HowMany).Value
Next HowMany
MsgBox HoursPerDay
End Sub
Does anyone have faced the similar problem or can someone explain to me what
is the cause of the"An unexpected error occurred with the method"
exception?
Regards
Jonathan
Sergey
2010-02-06 06:47:38 UTC
Permalink
The Project has a bug which is that it can't keep more than 10 TimeScaleValue s alive. The official recomendation is to use GC.Collect() to release the references, but I prefere the following, which is far more effective:

private void ReleaseComObject(object obj)
{
if (obj != null)
while (System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) > 0);


Posted via DevelopmentNow.com Group
http://www.developmentnow.com/g/

Loading...