excel - 使用 Excel VBA 返回 Outlook MAPIFolder 类型

标签 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 object variable or with block variable not set



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

最佳答案

你做错了。甚至 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

关于excel - 使用 Excel VBA 返回 Outlook MAPIFolder 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53952752/

相关文章:

vba - 更改存档所有工作表中的单元格公式

excel - 如何*快速*将许多 .txt 文件转换为 .xls 文件

javascript - VBA点击悬停下拉菜单

excel - 从url下载图片并保存到以单元格命名的文件夹中

HTML 代码在 Outlook 2010 上无法正确呈现

vba - Excel Workbook 对象中的哪些内容会增加文件大小?

vba - 如何使用 Apple 脚本访问 Powerpoint(Mac 版)的对象事件?

c# - 我可以用 C# dll 终止 VBA 宏执行吗

c# - 阻止 Outlook 将 HTML 转换为 RTF

outlook - 如何将 ICS session 自动添加到组织者的日历中?