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.
<SAMPLE CODE>
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
Next
If pub Is Nothing Then
MsgBox "No Public Folder store found for the default exchange mailbox"
End If
End If
</SAMPLE CODE>
Hope this helps!!! Feel free to ask questions related to Outlook/Exchange development.
Comments
- Anonymous
September 01, 2010
Hi and Thanks - have been searching for info on this all day.I need to address named public folders (from variables) in Access vba in order to create public tasks. We are Office 2010 now and Exchange under SBS 2008.I previously used the code below, which saw the same error as you report above.I guess I need to combine your snippet above.I get that I need to adress the Public Folder by my Account.SMTPAddress but wonder how I navigate down to a specific folder within the Public Store.Old 2007 Code ====Dim ObjAPP As Outlook.ApplicationDm objSystem_TaskFolder As Outlook.MAPIFolderDim objNewTask As Outlook.TaskItemSet ObjAPP = CreateObject("Outlook.Application")Set objNS = ObjAPP.GetNamespace("MAPI")Set objSystem_TaskFolder = _ objNS.Folders.Item(g_Public_Folders). _ Folders.Item(g_All_Public_Folders). _ Folders.Item(g_myProperty). _ Folders.Item(g_myTasks)then Set objNewTask = _ objSystem_TaskFolder.Items.AddWith ThanksGlenn Baxter - Anonymous
September 01, 2010
The comment has been removed - Anonymous
December 20, 2010
Below are segments of my code for an Outlook Form for time off request. I need to adapt this for Outlook 2010; can someone please show me the way?Thanks in advance!'-----This section is toward the top of my code----Sub InitOpts()' set user options 'public Time Off folder name and path mstrVacFolder = "Public Folders/All Public Folders/Time Off"End SubFunction GetMAPIFolder(strName) Dim objApp Dim objNS Dim objFolder Dim objFolders Dim arrName Dim objExpl Dim I Dim blnFound Set objApp = Application Set objNS = objApp.GetNamespace("MAPI") arrName = Split(strName, "/") Set objFolders = objNS.Folders blnFound = False For I = 0 To UBound(arrName) For Each objFolder In objFolders If objFolder.name = arrName(I) Then Set objFolders = objFolder.Folders blnFound = True Exit For Else blnFound = False End If Next If blnFound = False Then Exit For End If Next If blnFound = True Then Set GetMAPIFolder = objFolder Else Set GetMAPIFolder = Nothing End If Set objApp = Nothing Set objNS = Nothing Set objFolder = Nothing Set objFolders = Nothing Set objExpl = NothingEnd FunctionFunction GetFolderPath(objFolder)' from Randy Byrne, Building Applications with Outlook 2000 On Error Resume Next Dim strFolderPath Dim objChild Dim objParent strFolderPath = "" & objFolder.name Set objChild = objFolder Do Until Err <> 0 Set objParent = objChild.Parent If Err <> 0 Then Exit Do End If strFolderPath = "" & objParent.name & strFolderPath Set objChild = objParent Loop GetFolderPath = strFolderPath Set objChild = Nothing Set objParent = NothingEnd FunctionFunction GetCalFolder() Dim objFolder Dim objNS Set objNS = Application.GetNamespace("MAPI") Set objFolder = objNS.GetDefaultFolder(olFolderCalendar) mstrVacFolderViewName = objFolder.CurrentView GetCalFolder = GetFolderPath(objFolder) Set objFolder = Nothing Set objNS = NothingEnd Function - Anonymous
January 18, 2011
DO you happen to have the full code snippet for access to fucntion. Where can i get documentation on the library. - Anonymous
January 09, 2013
I use this :strFolder = appOutlook.Session.GetDefaultFolder(olPublicFoldersAllPublicFolders).ParentThis will return the correct public folder. No need to verify Outlook version or concatenate user to folder name. - Anonymous
January 10, 2013
Great post, thank you. I'd been having trouble with this.My solution to the long folder paths was to just drop the folders that I care about in the Favorites for the Public Folders. - Anonymous
February 07, 2013
Great info - thanks everyone. You have helped me convert old VB script to Outlook 2010 VBA - Anonymous
February 12, 2014
As someone else wrote before me, you only have to useOutlook.MAPIFolder pubRoot = appOutlook.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olPublicFoldersAllPublicFolders);to get a reference to the public folders.Don't use the advise of this article because it's not as simple.Think of international versions of Office where the string "Public Folders" is translated.If you are targeting also version 2007, the string that needs to be appended isn't the SMTP email address but the Display Name of the user from the address book. Which may or may not be maintained for some users (I have seen this happening).So just use GetDefaultFolder with the Outlook.OlDefaultFolders.olPublicFoldersAllPublicFolders enumerator and focus on more important stuff :)Also,"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..."is inaccurate...