我在Word 2010中编写了一些示例代码,但在添加了10个变量后它就崩溃了。我现在正在尝试理解VB中的ArrayLists。
If (ActiveDocument.Name = "template.docm") Then
With ActiveDocument
On Error Resume Next
.Variables.Add Name:="1", Value:="1"
.Variables.Add Name:="2", Value:="2"我认为代码开始向ArrayList添加对象,但我在VBA ArrayList上读到的所有内容都需要这样的声明:
Dim Variables As Object
Set Variables = CreateObject("System.Collections.ArrayList") 如果我遵循这个模式,使用.Variables.add创建更多的对象,那么索引在10之后就会中断,现在我只是想理解这个列表。
发布于 2019-05-21 02:30:02
variables是Document对象的集合属性,而不是ArrayList,在添加10个变量后似乎不会中断,至少在我的系统上是这样。
这帮助我找出了问题所在,因为没有声明数组/集合。
我的实际问题是在后来的循环中迭代。我之前有过:
Dim z As String
int i = 1
For Each f In myarray
.Variables(i).Value = f
i = i + 1
Next f并且为索引变量传递的参数实际上是一个字符串
.Variables(name) 所以:
int i = 1
For Each f In myarray
z = CStr(i)
.Variables(z).Value = f
i = i + 1
Next f修复了我的问题,感谢所有花时间帮助我的人!
https://stackoverflow.com/questions/56222346
复制相似问题