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

Lotus字符串数组非法边界求助:如何用变量定义数组?

解决LotusScript中字符串数组非法边界问题

LotusScript里固定大小数组的边界必须是常量,不能用变量定义,这就是你直接写Dim tablelabels(count) As String报错的核心原因。要实现动态大小的数组,得用动态数组结合ReDim语句处理,下面给两种可行方案:

方案1:先统计数量再初始化数组(推荐,性能更优)

先遍历一次文档统计总数,再用这个数值定义数组大小,避免频繁扩容操作:

Dim tablelabels() As String
Dim count As Integer
Dim doc As NotesDocument
Dim n As Integer

' 第一步:统计目标文档总数
count = 0
Set doc = view.GetFirstDocument()
While Not (doc Is Nothing)
    count = count + 1
    Set doc = view.GetNextDocument(doc)
Wend

' 初始化数组(数组下标从0开始,所以大小设为count-1)
ReDim tablelabels(count - 1) As String

' 第二步:遍历填充数组内容
n = 0
Set doc = view.GetFirstDocument()
While Not (doc Is Nothing)
    tablelabels(n) = doc.GetItemValue("Reply")(0)
    n = n + 1
    Set doc = view.GetNextDocument(doc)
Wend

方案2:动态扩容数组(无需提前统计数量)

如果不想遍历两次文档,可以在每次新增元素时用ReDim Preserve动态扩容数组,它会自动保留已有的数组数据:

Dim tablelabels() As String
Dim doc As NotesDocument
Dim n As Integer

n = 0
Set doc = view.GetFirstDocument()
While Not (doc Is Nothing)
    ' 扩容数组到当前下标n的位置
    ReDim Preserve tablelabels(n) As String
    tablelabels(n) = doc.GetItemValue("Reply")(0)
    n = n + 1
    Set doc = view.GetNextDocument(doc)
Wend

注意:ReDim Preserve每次都会重新分配内存并复制原有数据,当文档数量较多时,性能会比方案1差一些,可根据你的实际场景选择。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 12:18:28