R
redryderridesagain
Using Scripting dictionary in Word, it seems that a retrieval of a
non-existant item creates an item. In the code below the persons.count
= 1 after the failed retrieval. How would one lookup an item and add
one if not in the dictionary? Thanks.
Sub dictionarytest()
Dim c(2)
Dim persons As Object
Set persons = CreateObject("Scripting.Dictionary")
Dim keys As Variant, dcount As Long
On Error Resume Next
' This statement seems to add the item "foo" to the dictionary -
why?
dcount = persons("foo")(2)
On Error GoTo 0
dcount = persons.Count
MsgBox ("count1 = " & dcount)
c(1) = "foo"
dcount = persons.Count + 1
c(2) = dcount
' and it bombs here because "foo" is already in the dictionary
persons.Add "foo", c
End Sub
non-existant item creates an item. In the code below the persons.count
= 1 after the failed retrieval. How would one lookup an item and add
one if not in the dictionary? Thanks.
Sub dictionarytest()
Dim c(2)
Dim persons As Object
Set persons = CreateObject("Scripting.Dictionary")
Dim keys As Variant, dcount As Long
On Error Resume Next
' This statement seems to add the item "foo" to the dictionary -
why?
dcount = persons("foo")(2)
On Error GoTo 0
dcount = persons.Count
MsgBox ("count1 = " & dcount)
c(1) = "foo"
dcount = persons.Count + 1
c(2) = dcount
' and it bombs here because "foo" is already in the dictionary
persons.Add "foo", c
End Sub