找不到方法或数据成员错误

Method or Data Member not found Error

我正在尝试在 Outlook 宏上过滤我的组合框。但是我收到了标题中提到的错误。你能帮我解决这个问题吗? 它适用于 Visual Studio 但不适用于 Outlook 宏。

Private Sub cmbProjects_Change()
    Dim SelectedText As String
    SelectedText = cmbProjects.text

    Dim Items As Object
    Items = CreateObject("System.Collections.ArrayList")

    Items = cmbProjects.Items

    Dim newList As Object
    newList = CreateObject("System.Collections.ArrayList")
    For Each Item In Items
        If Item.Contains(SelectedText) Then
            newList.Add (Item)
        End If
    Next

    cmbProjects.DataSource = newList
End Sub

您需要添加 set,因为它是一个对象引用。并且您正在尝试使用其成员 methods/properties 访问此对象(您假设为 ArrayList)。哪个失败了。所以改为

Set Items = CreateObject("System.Collections.ArrayList")

Set newList = CreateObject("System.Collections.ArrayList")

进一步检查您是否在 VBA 编辑器中为集合库对象添加了正确的引用。

CreateObject("System.Collections.ArrayList")

根据我的评论,Items = cmbProjects.Items 也因同样的错误而失败。 That's because cmbProjects doesn't have a method called Items. So you need to use List instead.。列出 returns 一个变体对象。

因为您不能将变体转换为 ArrayList。您必须遍历变体并将项目添加到 ArrayList 中。注意我是即时编写的,代码没有经过测试。如果您需要进一步的帮助,您可以对其进行测试并发表评论。 我强烈建议您根据准确的语法验证此伪代码。

Dim vr as Variant
Dim i as Integer

vr = cmbProject.List

'--add error handling to check if vr is empty or if cmbProject is empty.
For i = 0 to UpperBound(vr)

   Items.Add (vr(i))
Next i