none
VBA to select mailbox if an account has multiple mailbox's RRS feed

  • Question


  • Here is my requirement.

    I have multiple accounts in my OUTLOOK configured. 1) 1@email.com (only one mailbox) 2) 2@email.com (Multiple mailbox's are there. ex: Unix box, Windows Box, Mac box)

    Now i need to select a folder in Unix box (inbox) and run the code to do something in side the folder.

    Here's what i have

    For Each oAccount In Application.Session.Accounts
    If oaccount ="1@email.com" then
    Set folder = ns.GetDefaultFolder(olFolderInbox) ' here it selects the inbox folder of account.
    For each item in folder.items
    Code goes here
    next
    end if
    next

    This works fine for single mailbox account, but when i do this for multiple mailbox account , it doesn't work.

    any help would be appreciated.


    • Moved by Bill_Stewart Saturday, November 28, 2015 2:22 AM Move to more appropriate forum
    Friday, November 27, 2015 10:13 AM

Answers

  • Hello Karthik,

    The code doesn't work because you always get the same folder from the GetDefaultFolder method of the Namespace class. It returns a Folder object that represents the default folder of the requested type for the current profile; for example, obtains the default Calendar folder for the user who is currently logged on. 

    The GetDefaultFolder method of the Store class returns a Folder object that represents the default folder in the store and that is of the type specified by the FolderType argument. This method is similar to the GetDefaultFolder method of the NameSpace object. The difference is that this method gets the default folder on the delivery store that is associated with the account, whereasNameSpace.GetDefaultFolder returns the default folder on the default store for the current profile.

    The Account class provides the DeliveryStore property which returns a Store object that represents the default delivery store for the account. So, you can use this property to get an instance of the Store class which provides the GetDefaultFolder method, for example:

     

    For Each oAccount In Application.Session.Accounts
      If oaccount ="1@email.com" then
        Set store = oaccount.DeliveryStore
        Set folder = store.GetDefaultFolder(olFolderInbox) 'here it selects the inbox folder of account.
        For each item in folder.items
          ' Code goes here
        next
      end if
    next

    You may find the Getting Started with VBA in Outlook 2010 article helpful.

    • Proposed as answer by jrv Saturday, November 28, 2015 5:24 PM
    • Marked as answer by David_JunFeng Monday, December 7, 2015 3:14 PM
    Saturday, November 28, 2015 3:01 PM

All replies

  • This works fine for single mailbox account, but when i do this for multiple mailbox accounts, it doesn't work.

    -> I feel flattered when I read phrases such as "It does not work". The poster obviously believes that repondents have x-Ray eyes that can observe the poster's screen right across the continents and figure out what exactly does not not work.

    Maybe all you need to do is to remove the "if" statement and indent your code so that you can see its structure.

    Set oOutlook = CreateObject("Outlook.Application")
    Set oNamespace = oOutlook.GetNamespace("MAPI")

    For Each oAccount In oOutlook.Session.Accounts
    '    If oAccount.SmtpAddress = "1@email.com" then
            Set oFolder = oNamespace.GetDefaultFolder(6)
            For each oItem in oFolder.items
                'Code goes here
            Next
    '    End if
    Next

    Friday, November 27, 2015 1:31 PM
  • Still wont work as oNameSpace will always point to the default account.  For each account you have to retrieve the store for the account and query the store, not the namespace.

    The easiest way to find how to do this is to do it in Outlook VBA and convert to VBScript.


    \_(ツ)_/

    Friday, November 27, 2015 7:27 PM
  • It is very easy to do this in PowerShell:

    $ol=New-Object -ComObject Outlook.Application
    for($i=1;$i -le $ol.Session.Accounts.Count;$i++ ){
    	Write-Host $ol.Session.Accounts.Item($i).DisplayName -fore green -NoNewLine
    	$ol.Session.Accounts.Item($i).DeliveryStore.GetDefaultFolder(6).Items.Count
    }


    \_(ツ)_/

    Friday, November 27, 2015 7:41 PM
  • Hello Karthik,

    The code doesn't work because you always get the same folder from the GetDefaultFolder method of the Namespace class. It returns a Folder object that represents the default folder of the requested type for the current profile; for example, obtains the default Calendar folder for the user who is currently logged on. 

    The GetDefaultFolder method of the Store class returns a Folder object that represents the default folder in the store and that is of the type specified by the FolderType argument. This method is similar to the GetDefaultFolder method of the NameSpace object. The difference is that this method gets the default folder on the delivery store that is associated with the account, whereasNameSpace.GetDefaultFolder returns the default folder on the default store for the current profile.

    The Account class provides the DeliveryStore property which returns a Store object that represents the default delivery store for the account. So, you can use this property to get an instance of the Store class which provides the GetDefaultFolder method, for example:

     

    For Each oAccount In Application.Session.Accounts
      If oaccount ="1@email.com" then
        Set store = oaccount.DeliveryStore
        Set folder = store.GetDefaultFolder(olFolderInbox) 'here it selects the inbox folder of account.
        For each item in folder.items
          ' Code goes here
        next
      end if
    next

    You may find the Getting Started with VBA in Outlook 2010 article helpful.

    • Proposed as answer by jrv Saturday, November 28, 2015 5:24 PM
    • Marked as answer by David_JunFeng Monday, December 7, 2015 3:14 PM
    Saturday, November 28, 2015 3:01 PM
  • The delivery store option provides the folders of the account that is specified in accounts.

    I need to access the folders of account that is not present in accounts but linked to one of the accounts in account settings.

    In the above code i get the folders of 1@email.com. I need folders of unix box(unix@box.com) which is linked to 1@email.com

    Did you understand the requirement here?

    Tuesday, December 1, 2015 9:27 AM
  • The above code processes all accounts in "Account Settings".  What is it that you do not understand about this?

    \_(ツ)_/

    Tuesday, December 1, 2015 10:50 AM
  • In that case you can simply iterates over all stores and try to find the required one there, see the Stores property of the Namespace class. It returns a Stores collection object that represents all the Store objects in the current profile.
    Tuesday, December 1, 2015 10:57 AM
  • The delivery store option provides the folders of the account that is specified in accounts.

    I need to access the folders of account that is not present in accounts but linked to one of the accounts in account settings.

    In the above code i get the folders of 1@email.com. I need folders of unix box(unix@box.com) which is linked to 1@email.com


    Can you explain exactly how you have "linked" Unix box, Mac box & Windows box to an Outlook account?

    AFAIK there is a 1 to 1 relationship between Outlook acccounts and the Delivery Store/Folder where email for that account is received.

    Tuesday, December 1, 2015 5:15 PM
  • My apologies for the delayed response. I was working on something else.

    Let me explain again.

    There are multiple mailbox's (UNIX (unix@email.com), Windows (windows@email.com) & mac (mac@email.com)) and then my account karthik@email.com

    Now, I can only add my mailbox in  outlook. Once I add my mailbox, the other ones gets added automatically in outlook. But there is only one account in 'Outlook accounts' i.e karthik@email.com. 

    If i use deliverystore or defaultfolder i get only the folders under karthek@email.com/inbox


    Did I make it clear?
    Monday, December 21, 2015 11:47 AM
  • Sorry, but its still not clear to me.  I don't know what you mean by the term "mailbox".  I suspect the confusion results from the description interchangeably using the phrase "mailbox" with an SMTP email address.

    Do you mean that the UNIX, Windows and mac "mailboxes" are actually message stores that you are adding to the Outlook profile in use?

    Monday, December 21, 2015 12:00 PM
  • UNIX, Windows and mac  are three separate accounts that i cannot directly access or add to my outlook.

    Once i add my account in outlook these 3 accounts show up in outlook but not in outlook accounts.

    In outlook accounts there'll be only one account i.e my account(karthik@email.com) but in outlook i can see and access all the four accounts.

    Monday, December 21, 2015 12:22 PM
  • UNIX, Windows and mac  are three separate accounts that i cannot directly access or add to my outlook.

    Once i add my account in outlook these 3 accounts show up in outlook but not in outlook accounts.

    In outlook accounts there'll be only one account i.e my account(karthik@email.com) but in outlook i can see and access all the four accounts.

    This explanation still doesn't make sense.  In the context of the Outlook Object Model an there can be multiple Account objects but each one references only one email address.

    Perhaps some screen shots that demonstrate what you are talking about would clarify.  Otherwise, I haven't got a clue what you mean by all this.

    Monday, December 21, 2015 12:35 PM
  • Same problem for me.  My Outlook is set up where I have two "mailboxes" in the folder pane.

    > Me@email.com

    > Public@email.com

    When I do

    For Each Account In OutlookApp.Session.Accounts

    it only finds Me@email.com, when I do

    Debug.Print OutlookApp.Session.Accounts.Count

    1

    Why can I not find the other account in the session?

    (Edited)  I went back into Control Panel --> Mail Setup and added the Public@email.com account.  Duh!


    • Edited by CharLeng Wednesday, August 28, 2019 6:20 PM
    Wednesday, August 28, 2019 3:46 PM
  • try this!

    For each Account in Application.Session.Stores

         Debug.print Account

    next Account

    Thursday, September 26, 2019 11:08 AM
  • I might be missing something, but this only lists all email accounts. Doesn't email agenda.
    Monday, January 27, 2020 6:24 PM
  • EXCELENT  THANKS !
    Thursday, August 27, 2020 4:01 AM