4

When accessing the members of the WIA Vector object directly using the Vector's Item property, or using the default property, they are seen by JScript as numbers:

var v = new ActiveXObject('WIA.Vector');
v.SetFromString('This is a test', true, false);
for (var i = 1; i<= v.Count; i+=1) {
    WScript.Echo(String.fromCharCode(v(i)));
}

However, if I use an Enumerator to iterate through the Vector:

var enumerator = new Enumerator(v);
enumerator.moveFirst();
while (!enumerator.atEnd()) {
    WScript.Echo(String.fromCharCode(enumerator.item()));
    enumerator.moveNext();
}

I get the following error under WScript / CScript:

enumerator.item() is not a number

typeof returns unknown:

WScript.Echo(typeof enumerator.item());

Presumably it's some sort of Automation type (as in this question) that only appears as a number, which is why typeof returns unknown.

How can I coerce this value to a "real" Javascript number?


Update

The following code in VBA:

Dim v As New Vector, item As Variant
v.SetFromString "This is a test", True, False
For Each item In v
    Debug.Print TypeName(item)
Next

prints

Byte
Byte
...

So the question can be narrowed down to the Byte specific Automation type.

Zev Spitz
  • 13,950
  • 6
  • 64
  • 136

1 Answers1

0

It works for me on IE11's console. (You have a mistake on the while loop)

The code:

console.log("- Vector -");
var v = new ActiveXObject('WIA.Vector');
v.SetFromString('This is a test', true, false);
for (var i = 1; i<= v.Count; i++) {
    console.log(v(i), String.fromCharCode(v(i)), typeof v(i));
}

console.log("- Enumerator -");
var e = new Enumerator(v);
e.moveFirst();
while (!e.atEnd()) {
    console.log(e.item(), String.fromCharCode(e.item()), typeof e.item());
    e.moveNext(); // You need this
}

Will print to the console:

- Vector -
84 T number
104 h number
105 i number
115 s number
32   number
105 i number
115 s number
32   number
97 a number
32   number
116 t number
101 e number
115 s number
116 t number
0  number
- Enumerator -
84 T number
104 h number
105 i number
115 s number
32   number
105 i number
115 s number
32   number
97 a number
32   number
116 t number
101 e number
115 s number
116 t number
0  number
GramThanos
  • 3,572
  • 1
  • 22
  • 34
  • How did you get this to work under IE? I get `Automation server can't create object`. In any event, this may work under IE, but it doesn't work under WScript / CScript. – Zev Spitz Apr 01 '18 at 16:06
  • The problem is not in the missing `.moveNext` call; if it was, the code would not error, it would continue in a never-ending loop. – Zev Spitz Apr 01 '18 at 16:29
  • Yes, I know the problem was not the moveNext, that is why I just said that it works for me (the missing moveNext just produce an infinite loop). To execute it on IE, https://stackoverflow.com/questions/15686040/ie9-automation-server-cant-create-object-error-while-using-certenroll-dll – GramThanos Apr 01 '18 at 18:42