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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/635558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 10:16:29  来源:igfitidea点击:

Accessing another maibox in outlook using vba

vbaoutlookoutlook-vba

提问by AntonioCS

I have two mailboxes in my Outlook.

我的 Outlook 中有两个邮箱。

One that is mine and it automatically logs me in when I log in to my pc and another I have that is for mail bounces.

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

I really need to access the inbox of the mail's account but I just can't seem to do it.

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

And there is no way I can make the mailbox of the mail account to be my default mailbox

并且没有办法让邮件账户的邮箱成为我的默认邮箱

Here is the code I have so far:

这是我到目前为止的代码:

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("[email protected]")
    myRecipient.Resolve
    If myRecipient.Resolved Then
        MsgBox ("Resolved")
        Set aFolder = ns.GetSharedDefaultFolder(myRecipient, olFolderInbox)
    Else
        MsgBox ("Failed")
    End If

End Sub

The problem I am getting is at the

我遇到的问题是

Set aFolder = ns.GetSharedDefaultFolder(myRecipient, olFolderInbox)

Set aFolder = ns.GetSharedDefaultFolder(myRecipient, olFolderInbox)

I get the Resolved msgbox so I know that is working but after that I get an error:

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

Run-Time Error

运行时错误

which doesn't say much about the error itself.

这并没有说明错误本身。

Can anyone help me out here please? Thanks

有人可以帮我吗?谢谢

采纳答案by Fionnuala

If the folder you wish to access is not an Exchange folder, you will need to find it, if it is an Exchange folder, try logging on to the namespace.

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

Log on to NameSpace

登录到命名空间

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

Find FolderAs far as I recall, this code is from Sue Mosher.

查找文件夹据我所知,此代码来自 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