Link to home
Start Free TrialLog in
Avatar of newbie46
newbie46

asked on

How can I search OutlooknameSpace.Folders to find the needed Shared Mailbox using VBA?

I am importing emails from Outlook 2010 using Access 2010. The emails are imported from a Shared mailbox ABC. Each user of the database has differing numbers of mailboxes on their machines. Thus, the Shared mailbox ABC may be accessed via:
Set Inbox = OutlookNameSpace.Folders(3).Folders("Inbox").Folders("New Entries") within one user's machine and may be accessed via:
Set Inbox = OutlookNameSpace.Folders(4).Folders("Inbox").Folders("New Entries")  within another user's machine.

I am looking for a way to search through the OutlookNameSpace.Folders using VBA to find the correct Shared mailbox ABC. When I use the following code, the error is trapped the first time and execution returns to FindMailboxFolder. If the folder is not found the 2nd time through, the error is no longer trapped within the code. Instead an error is displayed and processing within VBA is halted.

How can I search through OutlookNameSpace.folder dynamically in order to find the Shared mailbox on each user's machine?

Dim objOutlook As Object    'Outlook.Application
Dim OutlookNameSpace As Object
dim MailboxFolderNumber as Integer

On Error GoTo Err_ImportEmails_Click

Set objOutlook = GetObject(, "Outlook.application")
Set OutlookNameSpace = objOutlook.GetNamespace("MAPI")

MailboxFolderNumber = 3

FindMailboxFolder:
Set Inbox = OutlookNameSpace.Folders(MailboxFolderNumber).Folders("Inbox").Folders("New Entries")  
.
.
.
Exit_ImportEmails_Click:
  Set objOutlook = Nothing
  Set OutlookNameSpace = Nothing
  Exit Sub
  
Err_ImportEmails_Click:
  If Err.Number = -2147221233 Then
    MailboxFolderNumber = MailboxFolderNumber + 1
    GoTo FindMailboxFolder
  Else
   MsgBox Err.Number & vbCrLf & Err.Description, , "ImportEmails_Click"
  End If
 Resume Exit_ImportEmails_Click

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Bottomley
Chris Bottomley
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of newbie46
newbie46

ASKER

This worked. Thank you!!