0

I'm trying to pull in all the emails from a shared mailbox we use and I tried the below and its working, but from my main/default inbox only.

I've been trying to get this to work from the shared mailbox and just cant seem to get it working. I'm not a VBA expert and pulled this together from other threads so any help would be appreciated :)

Sub GetFromOutlook()

Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer

Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox)


i = 1

For Each OutlookMail In Folder.Items
    If OutlookMail.ReceivedTime >= Range("From_date").Value Then
        Range("eMail_sender").Offset(i, 0).Value = OutlookMail.SenderName
        Range("eMail_date").Offset(i, 0).Value = OutlookMail.ReceivedTime
        Range("eMail_subject").Offset(i, 0).Value = OutlookMail.Subject
        'Range("eMail_Recipients").Offset(i, 0).Value = OutlookMail.Recipients
        Range("eMail_text").Offset(i, 0).Value = OutlookMail.Body

        i = i + 1
    End If
Next OutlookMail

Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing

End Sub

I tried this as well, but couldn't get it to work:

Sub GetFromOutlook()

Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer


Dim olShareName As Outlook.Recipient


Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set olShareName = OutlookNamespace.CreateRecipient("MailboxName")

Set Folder = OutlookNamespace.GetSharedDefaultFolder(olShareName, olFolderInbox).Folders("Mailbox@XYZ.com").Folders("Inbox")
VBAWARD
  • 71
  • 1
  • 12
  • Possible duplicate of [Get reference to additional Inbox](https://stackoverflow.com/questions/9076634/get-reference-to-additional-inbox) – niton Nov 09 '18 at 17:00

1 Answers1

0

It looks like you need to set your Folder variable to the shared inbox before searching for olFolderInbox.

This is what works for me:

Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Outlook.Namespace
Dim targetFolder As Outlook.MAPIFolder
Dim firstFolder As Outlook.MAPIFolder
Dim olMail As Outlook.MailItem

Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set firstFolder = OutlookNamespace.Folders("your shared mailbox name")
Set targetFolder = firstFolder.Folders("Inbox")

For spot = 1 To 500
    If TypeOf targetFolder.Items(spot) Is MailItem Then
        Set olMail = targetFolder.Items(spot)
        If olMail.ReceivedTime > .... Then

        ......

        End If
    End If
Next
Kubie
  • 1,551
  • 3
  • 12
  • 23
  • It worked! Thank you so much! So the issue was that the shared inbox had to be defined as a variable? – VBAWARD Nov 09 '18 at 18:15
  • 1
    @VBAWARD this is the less flexible option described in https://stackoverflow.com/questions/9076634/get-reference-to-additional-inbox where the mailbox must be in the navigation pane. The more flexible option you tried using GetSharedDefaultFolder is also described there. – niton Nov 13 '18 at 16:59