vba - 使用vba访问outlook中的另一个邮箱

标签 vba outlook

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

我遇到的问题是

设置 aFolder = ns.GetSharedDefaultFolder(myRecipient, olFolderInbox)

我得到了 Resolved msgbox,所以我知道这是有效的,但在那之后我得到了一个错误:

Run-Time Error

这并没有说明错误本身。

有人可以帮我吗? 谢谢

最佳答案

如果您要访问的文件夹不是 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

关于vba - 使用vba访问outlook中的另一个邮箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/635558/

相关文章:

string - 比较两个字符串的元素

excel - 按值对 Excel 列进行排序

c# - 使用十六进制更改 Excel 单元格的背景色会导致 Excel 在电子表格中显示完全不同的颜色

Delphi在outlook msg中显示嵌入图像

excel - 根据标题名称移动列

Excel VBA : Setting a Cell Address to a variable "Object Required Error"

javascript - 准备 Outlook 邮件正文时出现 %0A 问题

html - Outlook 的电子邮件模板 - 填充/距离的技巧是什么?

html - 那里正在使用哪些电子邮件客户端?

css - Outlook:表格左对齐问题