使用Excel VBA返回Outlook MAPIFolder类型

时间:2018-12-28 01:38:18

标签: excel vba outlook

如果使用以下代码在我的Outlook目录集中不存在该文件夹,则创建一个文件夹。

Private Sub addOutlookFolderIfNotExists()
     Set apOutlook = CreateObject("Outlook.Application")
     apOutlook.Session.Logon
     Dim myNameSpace As Outlook.Namespace
     Dim myFolder As Outlook.Folder
     Dim myNewFolder As Outlook.Folder

     Set myNameSpace = apOutlook.GetNamespace("MAPI")
     Set myFolder = 
    myNameSpace.GetDefaultFolder(olFolderInbox).Parent.Folders("Estimates")
     For i = 1 To myFolder.Folders.Count
        If myFolder.Folders.Item(i).Name = "Testing" Then
           Exit Sub
        End If
     Next

     addOutlookFolderIfNotExists = myFolder.Folders.Add("Testing")
End Sub

之后我想使用文件夹的属性。我想返回刚刚创建的MAPIFolder对象。我将sub更改为如下所示的函数。

Private Function addOutlookFolderIfNotExists() As MAPIFolder
    Set apOutlook = CreateObject("Outlook.Application")
    apOutlook.Session.Logon
    Dim myNameSpace As Outlook.Namespace
    Dim myFolder As Outlook.Folder
    Dim myNewFolder As Outlook.Folder

    Set myNameSpace = apOutlook.GetNamespace("MAPI")
    Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox).Parent.Folders("Estimates")
    For i = 1 To myFolder.Folders.Count
        If myFolder.Folders.Item(i).Name = "Testing" Then
            'Debug.Print TypeName(myFolder.Folders.Item(i))
            addOutlookFolderIfNotExists = myFolder.Folders.Item(i)
            Exit Function
        End If
    Next

    addOutlookFolderIfNotExists = myFolder.Folders.Add("Testing")
End Function 

这将返回错误

  

vba对象变量或未设置块变量

但我不知道它指的是什么。

1 个答案:

答案 0 :(得分:1)

您做错了。甚至For循环也不正确。设置或分配对象的正确方法是使用命令SET

这是您要尝试的吗?

Private Function addOutlookFolderIfNotExists() As MAPIFolder
    Dim myNameSpace As Outlook.NameSpace
    Dim myFolder As Outlook.Folder
    Dim myNewFolder As Outlook.Folder
    Dim i As Long

    Set apOutlook = CreateObject("Outlook.Application")

    apOutlook.Session.Logon

    Set myNameSpace = apOutlook.GetNamespace("MAPI")
    Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox).Parent.Folders("Estimates")

    For i = 1 To myFolder.Folders.Count
        If myFolder.Folders.Item(i).Name = "Testing" Then
            '~~> Set the folder
            Set addOutlookFolderIfNotExists = myFolder.Folders.Item("Testing")
            Exit Function
        End If
    Next

    '~~> Create the folder
    myFolder.Folders.Add ("Testing")

    '~~> Set the folder
    Set addOutlookFolderIfNotExists = myFolder.Folders.Item("Testing")
End Function

您也可以在没有For循环的情况下执行上述操作。我们将使用On Error Resume Next代替。

Private Function addOutlookFolderIfNotExists() As MAPIFolder
    Dim myNameSpace As Outlook.NameSpace
    Dim myFolder As Outlook.Folder
    Dim myNewFolder As Outlook.Folder
    Dim i As Long

    Set apOutlook = CreateObject("Outlook.Application")

    apOutlook.Session.Logon

    Set myNameSpace = apOutlook.GetNamespace("MAPI")
    Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox).Parent.Folders("Estimates")

    '~~> Create the folder if it doesn't exists
    '~~> If it exists then suppress the error message and continue
    On Error Resume Next
    myFolder.Folders.Add ("Testing")
    On Error GoTo 0

    '~~> Set the folder
    Set addOutlookFolderIfNotExists = myFolder.Folders.Item("Testing")
End Function