Accessing another maybox in Outlook using vba

I have two mailboxes in my Outlook.

The one that belongs to me, and it automatically registers me when I log on to my computer, and I have another one for mail failures.

I really need to access the mailbox of the mail account, but I just can't do it.

And I can not make the mailbox of the mail account my default mailbox

Here is the code that I still have:

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

The problem I get is on

Set aFolder = ns.GetSharedDefaultFolder(myRecipient, olFolderInbox)

I get the allowed msgbox, so I know that it works, but after that I get the error message:

Runtime error

which does not indicate the error itself.

Can someone help me here please? Thanks

+3
1

, , Exchange, , Exchange, .

NameSpace

  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
+3

Source: https://habr.com/ru/post/1704598/


All Articles