使用VBA在Outlook中访问另一个maibox [英] Accessing another maibox in outlook using vba

查看:145
本文介绍了使用VBA在Outlook中访问另一个maibox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Outlook中有两个邮箱.

I have two mailboxes in my 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("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,所以我知道它在起作用,但是之后我得到了一个错误:

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

运行时错误

Run-Time Error

关于错误本身并没有多说.

which doesn't say much about the error itself.

有人可以在这里帮助我吗? 谢谢

Can anyone help me out here please? Thanks

推荐答案

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

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.

登录到NameSpace

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

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

Find Folder As far as I recall, this code is from 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在Outlook中访问另一个maibox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆