1

我在处理由 AutoCAD 中的方法调用返回的 Variant 字符串数组时遇到问题。返回的数组看起来是 kosher,但是当我尝试引用数组中的元素,甚至在 For Each 语句中包含数组的名称时,我得到一个类型不匹配错误

这是代码:

Dim acApp 'As AutoCAD.AcadApplication
Dim acDoc 'As AutoCAD.AcadDocument
Dim acLyt 'As AutoCAD.AcadLayout

'Get the AutoCAD application...
On Error Resume Next
Set acApp = GetObject(, "AutoCAD.Application")
On Error GoTo 0
If (acApp Is Nothing) Then
  Set acApp = CreateObject("AutoCAD.Application")
End If

'Is there a drawing open? If not we'll need to open a new drawing...
If acApp.Documents.Count > 0 Then
  Set acDoc = acApp.ActiveDocument
Else
  Set acDoc = acApp.Documents.Add
End If

'Get a reference to the Model Space layout (always first)...
Set acLyt = acDoc.Layouts(0)

'Get the list of canonical media names ("A4", "A3" etc) for the plot device for this layout...
'The AutoCAD documentation says that this method returns a variant, which is an array of strings,
'which seems to be what is actually returned.'
'However, I can't reference the array elements without producing a "Type Mismatch" error.

Names = acLyt.GetCanonicalMediaNames()

WScript.Echo VarType(Names) 'This line runs ok, and returns 8200, which is 8192 for Variant Array, + 8 for String.
WScript.Echo Names(0) 'This line generates the error...

我很困惑,所以任何帮助将不胜感激。

保罗

4

2 回答 2

1

至少有两个 StackOverflow 问题的答案表明 VBScript 只能处理从 COM 对象返回的变体数组。如果 AutoCAD 真的返回一个字符串数组,那么可能无法在 VBScript 中使用该数组(假设让 AutoCAD 更改其 COM 接口不是一种选择)。

参考:

于 2013-07-26T04:55:03.280 回答
0

它可能是一个多维数组。使用以下方法进行测试UBound

Ubound(Names, 1)  ' Number of Columns
Ubound(Names, 2)  ' Number of Rows

或更多(最多 32 个)。

于 2013-07-26T01:01:06.237 回答