Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm having trouble processing a Variant array of strings, returned by a call to method in AutoCAD. The returned array looks to be kosher, but when I try to reference elements in the array, or even include the name of the array in a For Each statement, I get a Type Mismatch error

Here is the code:

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...

I'm baffled, so any help would be appreciated.

Paul

share|improve this question
add comment

2 Answers

There are at least two StackOverflow questions whose answers indicate that VBScript can only handle Arrays of Variants returned from COM objects. If AutoCAD is really returning an Array of Strings, then there may be no way to consume the array in VBScript (assuming that getting AutoCAD to change their COM interface is not an option).

References:

share|improve this answer
    
Many thanks Cheran. That is a subtlety that eluded me. I think that this approach is doomed. Regards, Paul –  pdr0663 Jul 26 '13 at 5:38
add comment

It might be a multi-dimensional array. Test this using UBound:

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

or more (up to 32).

share|improve this answer
    
Thanks for your answer. I tried those tests, and the former succeeded where the latter did not. It's only a single-dimensioned array. –  pdr0663 Jul 26 '13 at 5:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.