VBScript字符串数组 - 错误800A000D类型不匹配

时间:2013-07-26 00:53:30

标签: arrays string vbscript autocad mismatch

我在处理Variant数组字符时遇到问题,通过调用AutoCAD中的方法返回。返回的数组看起来像犹太洁食,但是当我尝试引用数组中的元素,或者甚至在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...

我很困惑,所以任何帮助都会受到赞赏。

2 个答案:

答案 0 :(得分:1)

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

参考文献:

答案 1 :(得分:0)

它可能是一个多维数组。使用UBound

进行测试
Ubound(Names, 1)  ' Number of Columns
Ubound(Names, 2)  ' Number of Rows

或更多(最多32个)。