在VBA中从另一个模块调用字典及其变量

时间:2015-12-06 14:03:34

标签: vba powerpoint

我有一个存储在一个模块中的字典(Module6):

Sub WebcamDict() 


    Dim dict As Object, Key, val 
    Set dict = CreateObject("Scripting.Dictionary") 


    Key = "14 Mile Hill North": val = Array("URL", "Title") 
    dict.Add Key, val 
    Key = "14 Mile Hill South": val = Array("URL", "Title") 
    dict.Add Key, val 
    Key = "Ash Fork East": val = Array("URL", "Title") 
    dict.Add Key, val 
    ... 

End Sub

在另一个模块中,我试图调用字典及其变量:

Sub Images() 
    Call Module6.WebcamDict 

    ComboBoxList = Array(CStr(ComboBox4), CStr(ComboBox5), CStr(ComboBox6), CStr(ComboBox7)) 


    i = 1 


    For Each Ky In ComboBoxList 
        ActiveWindow.Selection.SlideRange.Shapes("Webcam" & CStr(i)).Fill.UserPicture (dict.Item(Ky)(0)) 
        ActiveWindow.Selection.SlideRange.Shapes("Webcam" & CStr(i)).TextFrame.DeleteText 
        ActiveWindow.Selection.SlideRange.Shapes("Webcam" & CStr(i)).Line.Visible = msoFalse 
        ActiveWindow.Selection.SlideRange.Shapes("Webcam" & CStr(i) & "_Text").TextFrame.TextRange.Text = dict.Item(Ky)(1) 


        i = i + 1 


    Next 


    Set dict = Nothing 


End Sub

变量“i”和“Ky”似乎都可以正常测试 Debug.Print陈述。但是,我收到此行的错误(Object Required):

ActiveWindow.Selection.SlideRange.Shapes("Webcam" & CStr(i)).Fill.UserPicture (dict.Item(Ky)(0))

如果我只是将字典包含在同一模块中,它可以完美地工作,但是我需要从多个不同的模块中调用它,所以我需要能够以某种方式调用它和它的变量。有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:1)

从sub中删除Dim dict As Object(保持其他所有内容)。在模块的顶部放行

Public dict As Object

这会将dict转换为可在项目中的任何位置访问的全局变量。在当前代码中,当WebCamdict返回时,它是一个超出范围的局部变量。