在Visual Basic for Applications(VBA)中设置全局Scripting.Dictionary [英] Setting a global Scripting.Dictionary in Visual Basic for Applications (VBA)

查看:210
本文介绍了在Visual Basic for Applications(VBA)中设置全局Scripting.Dictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个附加字典的功能。只要 updateList 不正确,我想保留这个字典的内容。我现在有这样的设置:

 公共函数runPolluxFull(voerUit As Boolean,updateList As Boolean)

Dim dicTitle As Variable
Dim dicFound As Variable

如果updateList = True然后
设置dicTitle = CreateObject(Scripting.Dictionary)
设置dicFound = CreateObject (Scripting.Dictionary)

状态
设置ObjectsJSON = jsonlibPollux.parse(responseString)

使用dicTitle
.Add inc,ObjectsJSON。项目(title)
结束

与dicFound
.Add inc,ObjectsJSON.Item(description)
结束

Wend
如果

而当 voerUit 是true,发生以下情况:

  For i = 1 To dicTitle.Count 
klaar = findReplace( dicTitle.Item(i),x+ dicTitle.Item(i),dicTitle.Item(i)+ vbCrLf + vbCrLf + dicFound.Item(i))
下一个i

这里的问题是当这个功能结束时, dicTitle dicFound 被清除,而code> findReplace 函数获取空参数。



有没有办法使代码工作,还是一个很好的解决方法?

解决方案

您将需要添加对Microsoft Scripting Runtime的引用以使用像我已经使用的绑定。最好是您声明它们的方式,因为您可以更好地访问对象,并且可以更容易地查看成员变量等。



方法1 - 模块级别或公共变量对于字典



您可以执行此操作的一种方法是为您使用的脚本字典创建模块级变量:

  public gDicTitle As Scripting.Dictionary 
Public gDicFound As Scripting.Dictionary

公共函数runPolluxFull(voerUit As Boolean,updateList As Boolean)


如果updateList = True然后
设置gDicTitle =新的Scripting.Dictionary
设置gDicFound =新的Scripting.Dictionary

状态
设置ObjectsJSON = jsonlibPollux.Parse(responseString)

使用gDicTitle
.Add inc,ObjectsJSON.Item(title)
结束

带gDicFound
。添加c,ObjectsJSON.Item(description)
结束

Wend
如果
结束函数
pre>

方法2 - 静态引用字典



您还可以通过使字典 static 。这将保留在函数调用之间,如文档所述。

 公共函数runPolluxFull(voerUit As Boolean,updateList As Boolean)
静态gDicTitle As Scripting.Dictionary
静态gDicFound As Scripting.Dictionary

如果updateList = True然后
设置gDicTitle =新的Scripting.Dictionary
设置gDicFound =新Scripting.Dictionary

当状态
设置ObjectsJSON = jsonlibPollux.Parse(responseString)

使用gDicTitle
.Add inc,ObjectsJSON.Item(title )
结束

与gDicFound
.Add inc,ObjectsJSON.Item(description)
结束

Wend
结束如果
结束功能


I have a function that is appending a dictionary. I want to keep the contents of this dictionary as long as the updateList isn't true. I currently have it setup like this:

Public Function runPolluxFull(voerUit As Boolean, updateList As Boolean)

Dim dicTitle As Variable
Dim dicFound As Variable

If updateList = True Then
Set dicTitle = CreateObject("Scripting.Dictionary")
Set dicFound = CreateObject("Scripting.Dictionary")

While status
        Set ObjectsJSON = jsonlibPollux.parse(responseString)

        With dicTitle
        .Add inc, ObjectsJSON.Item("title")
        End With

        With dicFound
        .Add inc, ObjectsJSON.Item("description")
        End With

Wend
End If

And when voerUit is true the following happens:

For i = 1 To dicTitle.Count
klaar = findReplace(dicTitle.Item(i), "x" + dicTitle.Item(i), dicTitle.Item(i) + vbCrLf + vbCrLf + dicFound.Item(i))
Next i

The problem here is that when this function ends, dicTitle and dicFound are cleared, and the findReplace function gets fed empty arguments.

Is there anyway to makes to code work, or a good workaround?

解决方案

You will need to add a reference to the Microsoft Scripting Runtime to use binding like I have used. It is preferable to the manner you are declaring them as you have better access to the object and can see member variables easier, etc.

Method 1 - module level or public variables for dictionaries

One way you can do this is to create module level variables for the scripting dictionaries you are using:

Public gDicTitle As Scripting.Dictionary
Public gDicFound As Scripting.Dictionary

Public Function runPolluxFull(voerUit As Boolean, updateList As Boolean)


    If updateList = True Then
        Set gDicTitle = New Scripting.Dictionary
        Set gDicFound = New Scripting.Dictionary

        While Status
                Set ObjectsJSON = jsonlibPollux.Parse(responseString)

                With gDicTitle
                .Add inc, ObjectsJSON.Item("title")
                End With

                With gDicFound
                .Add inc, ObjectsJSON.Item("description")
                End With

        Wend
    End If
End Function

Method 2 - static references to dictionaries

You can also do this by making the dictionaries static. This will preserve them between function calls like the documentation says.

Public Function runPolluxFull(voerUit As Boolean, updateList As Boolean)
    Static gDicTitle As Scripting.Dictionary
    Static gDicFound As Scripting.Dictionary

    If updateList = True Then
        Set gDicTitle = New Scripting.Dictionary
        Set gDicFound = New Scripting.Dictionary

        While Status
                Set ObjectsJSON = jsonlibPollux.Parse(responseString)

                With gDicTitle
                .Add inc, ObjectsJSON.Item("title")
                End With

                With gDicFound
                .Add inc, ObjectsJSON.Item("description")
                End With

        Wend
    End If
End Function

这篇关于在Visual Basic for Applications(VBA)中设置全局Scripting.Dictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆