我想显示从29/5到30/6但在间隔期间的所有工作时间。它包含星期六和星期日(非工作日)。我使用TimeScaleData
但是当它看到非工作日时会自动停止。我尝试将(1)添加到完成日但它仍然停止,因此我无法正确显示工作时间。
Microsoft.Office.Interop.MSProject.Application app = new Microsoft.Office.Interop.MSProject.Application();
app.FileOpenEx(Path, false, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
PjPoolOpen.pjPoolReadWrite, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Microsoft.Office.Interop.MSProject.Project pj = app.ActiveProject;
foreach(Resource re in pj.Resources)
{
foreach (Assignment assign in re.Assignments)
{
double h = 0;
var tsvs = assign.TimeScaleData("5/29/2017", "7/1/2017", MSProject.PjAssignmentTimescaledData.pjAssignmentTimescaledWork, MSProject.PjTimescaleUnit.pjTimescaleDays, 1);
try
{
foreach (TimeScaleValue tsv in tsvs)
{
for (int i = 0; i < totalday ; i++)
{
h += Convert.ToDouble(tsv.Value); //sum all value
}
}
}
catch { }
}
}
答案 0 :(得分:0)
您的代码在此行遇到运行时错误:h += tsv.Value
因为tsv.Value
是一个空字符串,几天没有工作。在尝试添加之前检查tsv.Value
的数据类型。
MSDN Documentation - 请注意,Value属性返回Variant,而不是double。
答案 1 :(得分:0)
在我的代码中,tsv.value在迭代timecalevalues时返回一个空字符串。检查IsNumeric是否有效:
Private Sub ProcessAssignments(T As Task)
Dim A As Assignment
Dim tsvsHours As TimeScaleValues
Dim tsvsCosts0 As TimeScaleValues
Dim dblWork As Double
Dim curCostClassA As Double
' Process assignments
For Each A In T.Assignments
' Get the timescale collection objects for Hours and Costs
tsvsHours = A.TimeScaleData(
StartDate:=T.BaselineStart,
EndDate:=T.BaselineFinish,
Type:=PjAssignmentTimescaledData.pjAssignmentTimescaledBaselineWork,
TimeScaleUnit:=PjTimescaleUnit.pjTimescaleMonths,
Count:=1)
tsvsCosts0 = A.TimeScaleData(
StartDate:=T.BaselneStart,
EndDate:=T.BaselineFinish,
Type:=PjAssignmentTimescaledData.pjAssignmentTimescaledBaselineCost,
TimeScaleUnit:=PjTimescaleUnit.pjTimescaleMonths,
Count:=1)
' Iterate through the assignment timescalevalues
For i As Integer = 1 To tsvsCosts0.Count
If IsNumeric(tsvsCosts0(i).Value) = True Then 'Cannot process non-working times with an empty string value
' Get the hours from the tsvsHours collection
If IsNumeric(tsvsHours(i).Value) Then
dblWork = CDbl(tsvsHours(i).Value / 60)
Else
dblWork = 0
End If
' Get the costs from the Baseline collection
If IsNumeric(tsvsCosts0(i).Value) Then
curCostClassA = tsvsCosts0(i).Value
Else
curCostClassA = 0
End If
' Do stuff here
End If
Next i
tsvsHours = Nothing
tsvsCosts0 = Nothing
Next A
End Sub