PDA

View Full Version : Solved: Using MAPI in Excel to Access Outlook Public Folders



brettdj
06-17-2004, 08:43 PM
A consultant designed an Outlook survery form that pulls survery data out of messages from a specific Outlook folder into Excel

He used the MAPI object from Excel to access the Outlook messages. Two queries

1) Why use MAPI rather than Outlook?
2) I've tried early binding from Excel with the CDO Reference rather than late binding. I get a runtime error (MAPI_E_No_ Found) as I track down the folder sytem to "Minerals". For some reason the Folder points to "Favourites" rather than "All Public Folders". The consultant made reference to this issue in his code. If i use

Set MAPIfold = MAPIobj.InfoStores("Public Folders").RootFolder.Folders(2)

it works but i'd like to know why there is an issue



Cheers

Dave


--His Code--


Set oObject = CreateObject("MAPI.Session")
oObject.Logon , , False, False
Set oFolder = oObject.InfoStores("Public Folders")
Set oFolder = oFolder.RootFolder

' Strange thing you seem to have to do for the RootFolder of an InfoStore!

For i = 1 To oFolder.Folders.Count
If oFolder.Folders(i).Name = "All Public Folders" Then
Set oFolder = oFolder.Folders(i)
Exit For
End If
Next
Set oFolder = oFolder.Folders("Minerals")


---My code----


Sub GeteMail()
Dim MAPIobj As MAPI.Session, MAPIfold As MAPI.Folder
Set MAPIobj = New MAPI.Session
MAPIobj.Logon , , False, False

Set MAPIfold = MAPIobj.InfoStores("Public Folders").RootFolder.Folders("All Public Folders")
'my code fails below
Set MAPIfold = MAPIfold.Folders("Minerals")
End Sub

jamescol
06-17-2004, 10:32 PM
The Namespace object represents the messaging service provider or message store. Currently the only available message store in Outlook is MAPI, which allows access to all Outlook folders and items.

In your case, you should use

Set MAPIobj = CreateObject("MAPI.Session")


instead of


Set MAPIobj = New MAPI.Session


The latter is used when you are already within an established Outlook session, such as when working within Outlook itself. You may also consider including a logon command in case Outlook is not already running.


objSession.Logon ("<Your Profile Name Here>")


Getting to subfolders is not the easiest task. I use this syntax myself working with a specific subfolder, as it is easy to follow and debug.


Set olApp = CreateObject("Outlook.Application")
Set olns = ol.GetNameSpace("MAPI")
Set MyFolder1 = olns.Folders("Public Folders")
Set MyFolder2 = MyFolder1.Folders("All Public Folders")
Set MyFolder3 = MyFolder2.Folders("Minerals")


Another method you can use is to obtain the EntryID and StoreId of the folder and use them to directly set an object to the public folder you want. The only drawback is with 1) a distributed application where users may be using different Exchange servers, thus different StoreIDs, or 2) situations where users may be accessing replicated public folders where again, the StoreID will differ. You can eliminate the StoreID and simply use the EntryID, but searching for the PF might be slow in teh above two cases.

Also, if the PF is ever relocated to a different store, its EntryID will change as well as the StoreID.

So, to continue the example above:


Dim myEntryID As String
Dim myStoreID As String
Dim PublicMineralFolder As Outlook.MAPIFolder

myEntryID = myFolder3.EntryID
myStoreID = myFolder3.StoreID

'Save the above values as constants, and all you have to do is
'use the following code to set the Minerals folder

Set PublicMineralFolder = myOlApp.Session.GetFolderFromID(myEntryID, _
myStoreID)


Hope this helps!

Cheers,
James

Daniel Klann
06-17-2004, 11:13 PM
Brett,

TO add to what James wrote. I spent the last hour researching this (what the hell, it's Friday so I'm not working :yes ). Anyway, I think this may be of relevance:-

href=http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdo/html/_olemsg_infostores_collection_object.asp

In particular, this bit:-


Note The Item(searchValue) syntax uses the IMAPITABLE::FindRow method, which performs a search dependent on the current sort order of the table underlying the collection. Not all tables are sorted alphabetically. The Microsoft Exchange Public Folders folder, for example, is held in a nonalphabetic order, and you should access its subfolders using the Item(index) syntax.HTH
Dan

brettdj
06-18-2004, 08:25 AM
James & Dan

Thank you kindly for your help.

I've adopted the method that James used (but with my original early binding) to drill down to the correct folder

Much appreciated

Cheers

Dave