I was writing two HTAs (HTML Applications) today and for the first one, I needed to create a VBArray with the number five in it. Fortunately, I remembered that I wrote a page about this prior to my blog. Therefore, here is the snippet which I plan on including in the next version of jPaq:


// Returns the array as a VBArray.
Array.prototype.toVBArray = function() {
  var dict = new ActiveXObject("Scripting.Dictionary");
  for(var i = 0, len = this.length; i < len; i++)
    dict.add(i, this[i]);
  return dict.Items();
};

Here is an example of how to use it:


var avbArray = ([1,4,5]).toVBArray();

Of course, this code is only useful in Internet Explorer (HTML or HTA) or in a standalone JScript file. For all of you who have to deal with VBArrays in JavaScript, have fun!!! 8)


1 Comment

bruce · April 23, 2012 at 10:53 AM

thanks for the tip here – I was scratching my head trying to figure out how to pass a VBA array to javaScript code through a scriptControl. Your post got me on track…

http://ramblings.mcpher.com/Home/excelquirks/snippets/scriptcontrol/arrays

Bruce

Leave a Reply

Your email address will not be published. Required fields are marked *