How to get reference to Public Folder Store using Outlook Object Model for Outlook 2010?

On Outlook 2007 or previous version for the Outlook we can refer to the public folder as below:

 Set olns =Item. Application.GetNamespace("MAPI")
 Set pub = olns.Folders("Public Folders")

However, the above given code snippet would fail on Outlook 2010 by throwing exception "Run-time error -2147221233 (8004010f) The attempted operation failed. An object could not be found" because with Outlook 2010.

We have an option to configure multiple exchange account in the same profile and Outlook appends the user's SMTP address used to configure the account to the mailbox/public folder store name to identify each of the account and Outlook would show/refer to the mailbox/public folder  as shown in image below.


Here is the sample code snippet in VBA to get reference to Public Folder Store using Outlook Object Model for Outlook 2010:

 'NOTE: Following programming examples is for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. 
 'This sample code assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. This sample code is provided for the purpose of illustration only and is not intended to be used in a production environment. 
 If InStr(Item.Application.Version, "14") Then
 Set olns =Item. Application.GetNamespace("MAPI")
 Set accs = olns.Accounts
 For Each Account In accs
     If Account.AccountType = olExchange And Account.SmtpAddress = olns.Session.DefaultStore Then
         Set pub = olns.Folders("Public Folders" & " - " & Account.SmtpAddress)
         MsgBox "Got the Pub folders"
     End If
     If pub Is Nothing Then
         MsgBox "No Public Folder store found for the default exchange mailbox"
     End If
 End If

Hope this helps!!! Feel free to ask questions related to Outlook/Exchange development.