【问题标题】:Return Outlook MAPIFolder type using Excel VBA使用 Excel VBA 返回 Outlook MAPIFolder 类型
【发布时间】:2018-12-28 01:38:17
【问题描述】:

如果我的 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 对象变量或未设置块变量

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

【问题讨论】:

    标签: excel vba outlook


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-25
      • 2021-03-12
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-24
      相关资源
      最近更新 更多