You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在VB/Vb.NET中获取MS Project自定义字段值列表的全部项?

获取MS Project自定义字段值列表全部项的方法

MS Project的对象模型里确实没有直接返回自定义字段值列表所有项的GetAllItems类函数,只能通过CustomFieldValueListGetItem按索引逐个读取。不过你不用靠猜上限+捕获异常的方式终止循环,可以用CustomFieldValueListCount属性获取值列表的总项数,这样就能精准遍历所有项。

优化后的代码示例

Dim MSPApp As Application
Set MSPApp = oProjAct.Application
Dim valueCount As Integer
Dim currentArray() As String
Dim i As Integer

' 获取目标自定义字段的值列表总项数
valueCount = MSPApp.CustomFieldValueListCount(PjCustomField.pjCustomTaskText2)

' 初始化数组并遍历所有项
If valueCount > 0 Then
    ReDim currentArray(1 To valueCount)
    For i = 1 To valueCount
        currentArray(i) = MSPApp.CustomFieldValueListGetItem(PjCustomField.pjCustomTaskText2, PjValueListItem.pjValueListValue, i)
    Next i
End If

原代码的问题

你之前的代码依赖固定上限(1500)和异常捕获终止循环,存在两个隐患:

  • 如果值列表项数超过1500,会直接漏掉超出部分的项
  • 若循环过程中出现非“索引超出范围”的异常,会错误提前终止循环

用CustomFieldValueListCount获取总项数的方式更可靠,也更贴合MS Project对象模型的设计逻辑。

内容的提问来源于stack exchange,提问作者Anatoly Funikov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 23:53:15