Share via

Read Emails from a shared account in Outlook

Anonymous
2022-03-10T19:48:58+00:00

I have VBA code that reads the emails in a folder directly under the Inbox.

It works when the mailbox is my personal account on the server at work. I also have permissions to a shared mailbox on the server at work. I can send mail out on behalf of this shared account. However when I try to use the same code with this mailbox to read it, it cannot find an object; namely, it won't let me get to the mailbox for the purposes of reading the folders. The line the code fails on is highlighted.

Here is the code. The only difference between the version that works and the version that doesn't work is the mailbox name that I pass to the subroutine. My IT department blames it on the VB code. This same code also works with my 365 HOME Outlook and has worked with a former employer on a shared account on an exchange server.

What should I tell my IT department to do to make this work for me?

Option Explicit

Sub ReadMail(MailBox As String, SubFolder As String, sht As Worksheet)

' Mailbox = mailbox name

' Subfolder = folder directly under Inbox

' sht = the sheet with the output table

Dim olApp As Object ' Outlook Application

Dim olNS As Object ' Outlook Name Space

Dim FldrIn As Object ' Dock Schedule folder

Dim FldrOut As Object ' Dock Schedule processed folder

Dim olAtt As Object ' Outlook attachement

Dim k As Long ' Index to folder item

Dim FileName As String ' File names in folder

Dim RowNum As Long ' Row Number for Output Table

Const olMailItem As Long = 0

' Initalize variables

RowNum = 2

' Set up mailbox

Set olApp = CreateObject("Outlook.Application")

Set olNS = olApp.GetNamespace("MAPI")

Set FldrIn = olNS.Folders(MailBox).Folders("Inbox").Folders(SubFolder)

DoEvents

Set FldrOut = olNS.Folders(MailBox).Folders("Inbox").Folders(SubFolder)

Application.ScreenUpdating = False

Application.DisplayAlerts = False

' Go through mail items bottom to top

For k = FldrIn.items.Count To 1 Step -1

On Error Resume Next 

sht.Cells(RowNum, "A") = FldrIn.items(k).Sender 

sht.Cells(RowNum, "B") = FldrIn.items(k).Subject 

sht.Cells(RowNum, "C") = FldrIn.items(k).ReceivedTime 

sht.Cells(RowNum, "D") = FldrIn.items(k).Body 

On Error GoTo 0 

RowNum = RowNum + 1 

Next k

' Clean up Outlook

Set FldrIn = Nothing

Set olNS = Nothing

Set olApp = Nothing

Application.DisplayAlerts = True

Application.ScreenUpdating = True

Application.StatusBar = False

End Sub

Outlook | Windows | Classic Outlook for Windows | For home

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

26 answers

Sort by: Most helpful
  1. Diane Poremsky MVP 17,341 Reputation points MVP Volunteer Moderator
    2022-03-11T06:14:37+00:00

    >> Set FldrIn = olNS.Folders(MailBox).Folders("Inbox").Folders(SubFolder)

    That is the code for the default inbox.

    You need to get the shared folder list this -

     Set newCalFolder = NS.GetSharedDefaultFolder(objOwner, olFolderCalendar)
    

    Full code sample here - Working with VBA and non-default Outlook Folders (slipstick.com)

    Oh, and you won't need to change the how the shared mailbox is added to your profile, as long as its added as an account (using a username & password)

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2022-03-10T22:22:54+00:00

    I'll have to ask my IT department how they did it. I will pass on your comments to them.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2022-03-10T21:43:40+00:00

    Hi DanFlak,

    Thanks for the update. May I know how did you added shared mailbox automatically via automapping or have you manually added (via File > Account Settings > Account Settings…Change> More Setting..> Advanced> Add) in Outlook desktop client?

    If it is added automatically via automapping then remove the automapping via PowerShell and manually add or if it is manually added then try automapping method to check this issue.

    Your patience and understanding are highly appreciated.

    Best regards,

    Shyamal

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2022-03-10T21:26:08+00:00

    Thanks, I will give that a shot, but this is not a VBA issue. It has something to do with my permissions for the Outlook shared mailbox.

    Is there an Outlook Exchange group?

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2022-03-10T20:17:59+00:00

    Hi DanFlak,

    Thank you for posting. Since our forum doesn’t focus on VBA code related issue. However, we have dedicated Outlook developer support team for VBA code therefore I would suggest you please post your question in the office-vba-dev Microsoft Q&A Community forum and there experts’ will focus on the issue to further assist you.

    Regarding this point, we would truly appreciate your kind understanding.

    Best regards,

    Shyamal

    Was this answer helpful?

    0 comments No comments