Home > Back-end >  Select inboxes in different outlook accounts
Select inboxes in different outlook accounts

Time:01-27

How can I select the inbox of my secondary outlook account? The code below should work. But no matter if I choose account 1 or 2 (by changing the index in the outlookApp.Session.Accounts.Item(.) line), it still sets the Fldr to my first account. I know it's doing this because the message box always says 'Searched 4 items' even though there's more than four items in my secondary email.

Sub find_email2()

  Dim outlookApp
  Dim olNs As Outlook.Namespace
  Dim oAccount As Account
  Dim Fldr As Outlook.MAPIFolder
  Dim olMail As Variant
  Dim myTasks
  Dim sir() As String
  Dim attachmentFileName As String

  Set outlookApp = CreateObject("Outlook.Application")

  Set olNs = outlookApp.GetNamespace("MAPI")
  Set oAccount = outlookApp.Session.Accounts.Item(2) ''' makes no difference if this is 1 or 2

  Set Fldr = oAccount.Session.GetDefaultFolder(olFolderInbox)
  
  Set myTasks = Fldr.Items

  MsgBox "Searched " & myTasks.Count & " items"

End Sub

CodePudding user response:

You get the same folder because oAccount.Session points to the same Namespace object even if accounts are different.

You can use either Namespace.Stores collection and call Store.GetDefaultFolder(olFolderInbox) (be prepared to handle errors as not all stores expose the same default folders - e.g. Public Folders store does not have an Inbox) or use Namespace.Accounts collection and for each Account use Account.DeliveryStore.GetDefaultFolder(olFolderInbox)

  •  Tags:  
  • Related