Can't get a reference to a newly added store

W

Water Cooler v2

I am creating an Outlook add-in using VB.NET with VS.NET 2003. I add a
new store and then want to do something with it, but I can't seem to
get a reference to it. I put logging and found that I could get a
reference to all other stores and folders except the one I just added.

I also tried the same piece of code from within Outlook, and it worked.
I could get a valid reference for the *just* newly added store.

Here's the code I wrote in VB.NET:


Dim lSettings As Settings = GetPersistedSettings()
Dim nm As Outlook.NameSpace
Dim folders As Outlook.Folders
Dim fld As Outlook.MAPIFolder

nm = applicationObject.GetNamespace("MAPI")
nm.AddStore("Foo.pst")

Dim LngFolders As Long = nm.Folders.Count
Try
Log.Write("nm.Folders.Item(LngFolders) Is Nothing : " &
(nm.Folders.Item(LngFolders) Is Nothing))
fld = nm.Folders.Item(LngFolders)

Catch ex As Exception
Log.Write(ex.ToString & "Could not reference the newly
added Personal Folders collection.")
End Try



The code fails at the line:

Log.Write("nm.Folders.Item(LngFolders) Is Nothing : " &
(nm.Folders.Item(LngFolders) Is Nothing))


I've checked it thoroughly for the last two hours with different kinds
of logging. If I replace the LngFolders in the above line with, say, 1
like so:

Log.Write("nm.Folders.Item(1) Is Nothing : " & (nm.Folders.Item(1) Is
Nothing))

it is able to get the reference to the first (default) message store
that MAPI creates.



The same code, when modified for an Outlook macro, works from within
Outlook.


What gives?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top