1

In my VBA code i'm using the following:

Dim docsToSave As Scripting.Dictionary
Set docsToSave = New Scripting.Dictionary

Dim toRemove As Collection
Set toRemove = New Collection

...
More Code
...

For i = 1 To toRemove.Count
    docsToSave.Remove (toRemove.Item(i))
Next

The nice thing about Collections compared to Dictionaries is that you can retrieve keys by using their item numbers.
My problem is that I need to convert my VBA-code to CATScript (similar to VBScript).
I don't think it's easy to get a code as simple as the above for subtracting one list of "key, item" from another list.
What's the best way to fix my code so that it works in CAT/VB-Script?
I believe it may be possible with an Array, since I'm very new at coding in general I figured I'd ask here first for a better way before I try to make it work with Arrays.

Edit:
This question differs from CATIA VBA Enable use of “Scripting.Dictionary” in .CATScript because this question specifically requests information on how to use a dictionary in VBScript so that it would replicate the behaviour of a Collection used in an existing VBA-script.

Laurens Ruben
  • 109
  • 5
  • 22
  • Possible duplicate of [CATIA VBA Enable use of "Scripting.Dictionary" in .CATScript](https://stackoverflow.com/questions/48264708/catia-vba-enable-use-of-scripting-dictionary-in-catscript) – user692942 Jan 16 '18 at 18:55
  • [VBA and VBScript](https://msdn.microsoft.com/en-us/library/5z0bwhc7(v=vs.85).aspx) share [a lot of similarities](https://stackoverflow.com/q/1384051/692942) which means you should be able to convert the code without too many problems. – user692942 Jan 16 '18 at 18:59
  • "Collection" doesn't exist in VBScript, and the way I'm using a collection here is, unfortunately, not possible with a dictionary. – Laurens Ruben Jan 17 '18 at 06:43
  • VBScript isn’t strongly typed so it won’t know what `As Collection` is, everything is a `Variant` data type. But it supports COM so there is no reason you can’t access a COM visible `Collection` object – user692942 Jan 17 '18 at 06:52
  • Right, I'm missing some information on how this all works. Your comments are helpful. – Laurens Ruben Jan 17 '18 at 15:25

2 Answers2

2

Either you have to use arrays, and manage the resizing yourself as you add and delete items to/from it, or you can use a dictionary and manage the keys as integers. I usually do the latter.

'create dictionary
set dict = CreateObject("Scripting.Dictionary")

'add object
dict.add dict.count,objectOrValue

'loop
for i = 0 to dict.count -1
   objectOrValue = dict.item(i)
   ...
next

This should behave much like a zero-based collection if you want to keep the one-based behavior of the vba collection use "dict.count+1" as the key

C R Johnson
  • 964
  • 1
  • 5
  • 12
1

For the posted code to behave in the same way in VBScript as it would in VBA the following could be used:

Dim docsToSave
Set docsToSave = CreateObject("Scripting.Dictionary")

Dim toRemove
Set toRemove = CreateObject("Scripting.Dictionary")

...
More Code
...

For i = 0 To toRemove.Count - 1
    docsToSave.Remove (toRemove.keys()(i))
Next

Furthermore, to add to the Dictionary a different syntax is used compared to a Collection:

'VBA-code for a collection:
toRemove.Add (x)

'VBScript for a dictionary:
Call toRemove.Add(x, i)

These changes were sufficient for me to have my VBA-script work in VBScript.

Laurens Ruben
  • 109
  • 5
  • 22