编译错误:无法在重新输入数组字典对象的 VBA 函数中分配给数组)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14661839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:26:36  来源:igfitidea点击:

Compile error: Can't assign to array in VBA function that reutrns array dictionary objects)

vba

提问by 0fnt

I have the following code:

我有以下代码:

Function MC() As Object()

Dim RulesList(0 To 10) As Object

Dim Rule
Set Rule = CreateObject("Scripting.Dictionary")
Rule.Add "Sender", "Test"
Rule.Add "Subject", "bbb"
Rule.Add "Folder", "ccc"
Rule.Add "MarkRead", False

Set RulesList(0) = Rule


Set Rule = CreateObject("Scripting.Dictionary")
Rule.Add "Sender", "Java"
Rule.Add "Subject", "bbb"
Rule.Add "Folder", "ccc"
Rule.Add "MarkRead", False

Set RulesList(1) = Rule

Set MC = RulesList

End Function

in outlook VBA. The code throws

在展望 VBA。代码抛出

"Compile Error: Can't assign to array on line Set MC = RulesList

“编译错误:无法在线分配给数组 Set MC = RulesList

Can somebody help me here please? I want to create an array of dictionary objects and return them.

有人可以帮我吗?我想创建一个字典对象数组并返回它们。

EDIT: Removing () at the end of function signature and using MC = RulesList in place of Set MC = RulesList works, however, I can't assign this in my calling function anymore, can anyone point me to help on that?

编辑:在函数签名末尾删除 () 并使用 MC = RulesList 代替 Set MC = RulesList 工作,但是,我不能再在我的调用函数中分配它了,有人能指点我吗?

回答by Fionnuala

Some notes:

一些注意事项:

Function MC() As Object()
    Dim RulesList(0 To 10) As Object

    Set Rule = CreateObject("Scripting.Dictionary")
    Rule.Add "Sender", "Test"
    Rule.Add "Subject", "bbb"
    Rule.Add "Folder", "ccc"
    Rule.Add "MarkRead", False

    Set RulesList(0) = Rule


    Set Rule = CreateObject("Scripting.Dictionary")
    Rule.Add "Sender", "Java"
    Rule.Add "Subject", "bbb"
    Rule.Add "Folder", "ccc"
    Rule.Add "MarkRead", False
    Debug.Print Rule(1)

    Set RulesList(1) = Rule

    MC = RulesList

End Function

Sub getmc()
    Dim abc
    abc = MC
    Set Rule = abc(1)
    a = Rule.items
    Debug.Print a(0)
End Sub