代码之家  ›  专栏  ›  技术社区  ›  AntonioCS

使用vba访问Outlook中的另一个maibox

  •  3
  • AntonioCS  · 技术社区  · 15 年前

    我的Outlook中有两个邮箱。

    一个是我的,当我登录到我的电脑时,它会自动登录我,另一个是我的,用于邮件反弹。

    我真的需要访问邮件帐户的收件箱,但我似乎做不到。

    我无法将邮件帐户的邮箱设为默认邮箱

    这是我迄今为止的代码:

    Public Sub GetMails()
    
        Dim ns As NameSpace
        Dim myRecipient As Outlook.Recipient
        Dim aFolder As Outlook.Folders
    
        Set ns = GetNamespace("MAPI")
    
        Set myRecipient = ns.CreateRecipient("mail@mail.pt")
        myRecipient.Resolve
        If myRecipient.Resolved Then
            MsgBox ("Resolved")
            Set aFolder = ns.GetSharedDefaultFolder(myRecipient, olFolderInbox)
        Else
            MsgBox ("Failed")
        End If
    
    End Sub
    

    我遇到的问题是

    Set aFolder = ns.GetSharedDefaultFolder(myRecipient, olFolderInbox)

    我得到了已解决的msgbox,因此我知道这是有效的,但在此之后我得到了一个错误:

    运行时间误差

    这并不能说明错误本身。

    有人能帮我吗? 谢谢

    1 回复  |  直到 9 年前
        1
  •  3
  •   Tim Whitcomb    12 年前

    如果要访问的文件夹不是Exchange文件夹,则需要找到它;如果是Exchange文件夹,则尝试登录到命名空间。

    登录到命名空间

      Set oNS = oApp.GetNamespace("MAPI")
      oNS.Logon
    

    查找文件夹 据我所知,这个密码是苏莫舍写的。

    Public Function GetFolder(strFolderPath As String) As Object 'MAPIFolder
    ' strFolderPath needs to be something like
    '   "Public Folders\All Public Folders\Company\Sales" or
    '   "Personal Folders\Inbox\My Folder" ''
    
    Dim apOL As Object 'Outlook.Application '
    Dim objNS As Object 'Outlook.NameSpace '
    Dim colFolders As Object 'Outlook.Folders '
    Dim objFolder As Object 'Outlook.MAPIFolder '
    Dim arrFolders() As String
    Dim I As Long
    
    On Error GoTo TrapError
    
        strFolderPath = Replace(strFolderPath, "/", "\") 
        arrFolders() = Split(strFolderPath, "\")
    
        Set apOL = CreateObject("Outlook.Application")
        Set objNS = apOL.GetNamespace("MAPI")
    
    
        On Error Resume Next
    
        Set objFolder = objNS.Folders.Item(arrFolders(0))
    
        If Not objFolder Is Nothing Then
            For I = 1 To UBound(arrFolders)
                Set colFolders = objFolder.Folders
                Set objFolder = Nothing
                Set objFolder = colFolders.Item(arrFolders(I))
    
                If objFolder Is Nothing Then
                    Exit For
                End If
            Next
        End If
    
        Set GetFolder = objFolder
        Set colFolders = Nothing
        Set objNS = Nothing
        Set apOL = Nothing
    
    
    End Function
    
    推荐文章