Get Folders Collection after calling AddStore?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, Can someone tell me how to get a pointer to the folders collection of a PST that I just finished adding with AddStore or AddStoreEx if there's already several PSTs in the profile? I can't seem to figure out how to get a pointer to the store I just got done adding. If AddStore returned a MAPIFolder object I'd have no problem; but it doesn't appear that it does.
 
For that matter, how do I add a PST using AddStore and then later remove the same PST using RemoveStore? AddStore takes a file path whereas RemoveStore takes a MapiFolder object as a parameter. I can't figure out how to get that MapiFolder object. It would be nice if I could assume that when I call AddStore, the new store is added to the end of the NameSpace's list of FolderObjects. i.e.

Dim objOL As New Outlook.Applicatio
Dim objName As Outlook.NameSpac
Dim objFolder As Outlook.MAPIFolde

Set objName = objOL.GetNamespace("MAPI"
objName.AddStoreEx <path to PST>, olStoreUnicod

' Add the PST to the Profil

Set objFolder = objName.Folders.Item(objName.Folders.Count) <<<<------- Can I do this? Will this be guarnteed to work in furture versios of Outlook

' Remove the PST I just added

objName.RemoveStore objFolde
 
The top level folder of the new store is what you want to use in
RemoveStore.

I wouldn't bet that any newly added store has the highest index number
of the NameSpace.Folders collection, although so far in my experience
it always has. The sure fire way to make sure you get the correct
newly added store is to save all the StoreID's of all existing stores
before you add a new one. Then iterate the Namespace.Folders
collection and find the new StoreID that wasn't already saved. I
usually save things like that in a collection.




Rick Andrews said:
For that matter, how do I add a PST using AddStore and then later
remove the same PST using RemoveStore? AddStore takes a file path
whereas RemoveStore takes a MapiFolder object as a parameter. I can't
figure out how to get that MapiFolder object. It would be nice if I
could assume that when I call AddStore, the new store is added to the
end of the NameSpace's list of FolderObjects. i.e.,
Dim objOL As New Outlook.Application
Dim objName As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder

Set objName = objOL.GetNamespace("MAPI")
objName.AddStoreEx <path to PST>, olStoreUnicode

' Add the PST to the Profile

Set objFolder = objName.Folders.Item(objName.Folders.Count)
<<<<------- Can I do this? Will this be guarnteed to work in furture
versios of Outlook?
 
Wow. This is really clugy, but it seems like it will work. I can't believe, though, that you can load a new store and then not know anything about the store you just loaded. There must be some property someplace that points to the last store loaded. The StoreID helps me with another problem I'm having too. I've been relying on the folder name to navigate the folders, but often there's more than one folder with the same name, e.g., "Archive Folder" is the default name of just about all my archive PSTs.
 
There is a method: add a StoreAddEventHandler to your Outlook Application session, so you can retrieve the StoreId of the newly created Store in one way or another:

private void Method()
{
this.Application.Session.Stores.StoreAdd += StoreAddEventHandler;
}

private void StoreAddEventHandler(Store store)
{
//var sId = store.StoreId;
}
 
Back
Top