【问题标题】:Accessing another maibox in outlook using vba使用 vba 在 Outlook 中访问另一个邮箱
【发布时间】:2010-10-12 17:49:14
【问题描述】:

我的 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)

我得到 Resolved msgbox,所以我知道它正在工作,但之后我收到一个错误:

运行时错误

这并没有说明错误本身。

有人可以帮我吗? 谢谢

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

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

    登录命名空间

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

    查找文件夹 据我记得,这段代码来自 Sue Mosher。

    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
    

    【讨论】:

    • 哇!感谢您的代码。我设法通过创建一个新配置文件并仅指定我想要的帐户来解决我的问题,因此代码在该帐户上运行:) 谢谢
    猜你喜欢
    • 1970-01-01
    • 2018-11-20
    • 2013-04-03
    • 1970-01-01
    • 2014-10-06
    • 2022-05-24
    • 1970-01-01
    • 2023-03-11
    • 2014-04-24
    相关资源
    最近更新 更多