Problem working with folders

F

Frode Lillerud

Hello,

I'm trying to do some simple folder manipulation.

I'm able to create a folder, but I'm having some problems getting the code
to find it again the next time the program starts.

Right now I'm writing some code that for-loops through all the folder using
the .GetNext()-method, and checks whether the .Name-property is the one I'm
looking for.

The problem is that the .GetNext()-method always returns the __second__
folder, and doesn't iterate further.

For instance, having the following four folders: Folder1, Folder2, Folder3
and Folder4.
..GetFirst() always returns Folder 1
..GetNext() always returns Folder2
and
..GetLast() always returns Folder4

Here is a fragment of the code (in C#):


Outlook.MAPIFolder oInbox = olNameSpace.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderInbox);

oSWFolder = oInbox.Folders.GetFirst();
for (int counter = 1; Counter <= iTotalNumberOfFolders; counter++)
{
if (oSWFolder.Name == "MyFolder")
{
iSWFolderNumber = teller;
break;
}
else
{
oSWFolder = oInbox.Folders.GetNext();
}
}

Can anyone explain why I'm not able to get to Folder3??

and second question:
On a MSDN page I've read about a .Item(int) property
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cdo/html/
_olemsg_item_property_folders_collection_.asp), but there doesn't seem to
be one available to me.

How can I fast and easily select the folder when I know which number it
has? Like the iSWFolderNumber in the preceding code.

Thanks :)
 
D

Dmitry Streblechenko \(MVP\)

Everytime you call
oSWFolder = oInbox.Folders.GetNext();
Outlook returns a brand new instance of the Folders collection, so GetNext
always returns Folder2.
Cache oInbox.Folders in a separate variable to make sure that you are using
the same instance of Folders.
You can also use a "for" loop.

Outlook.MAPIFolder oInbox = olNameSpace.GetDefaultFolder
(Outlook.OlDefaultFolders.olFolderInbox);

oFolders = oInbox.Folders
oSWFolder = oFolders .GetFirst();
for (int counter = 1; Counter <= iTotalNumberOfFolders; counter++)
{
if (oSWFolder.Name == "MyFolder")
{
iSWFolderNumber = teller;
break;
}
else
{
oSWFolder = oFolders.GetNext();
}
}

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
F

Frode Lillerud

Thanks, that worked superbly!

But, on to the second question, how can I later use the iSWFolderNumber.
For instance, now I know that my folder is number 5.

Should I later be able to do something like this (pseudo-code):

public void MoveMailToFolder(MailItem mail, int iSWFolderNumber)
{
FolderToMoveTo = oFolders(iSWFolderNumber);
//code for copying mail into FolderToMoveTo
}

Frode
 
D

Dmitry Streblechenko \(MVP\)

No, it is not guaranteed that the folder index will remain the same. Either
store the folder entry id (MAPIFolder.EntryID) and then reopen it using
Namespace.GetFolderFromID or store the folder name and access the folder by
its name (hoping that it won't change):
ParentFolder.Folders.Item(SubfolderName).

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
F

Frode Lillerud

Yupp, I went for the EntryID solution. Seems to work fine.

I've read about the second solution (ParentFolder.Folders.Item
(SubfolderName)) somewhere before, but there isn't a .item available. Just
..items.

But, as long as I got the EntryID to work, thats enough for me.
Thanks Dmitry!
 
D

Dmitry Streblechenko \(MVP\)

Note that Items is a collection property of the MAPIFolder object.
MAPIFolder.Folders.Item (note .Folders) is the default property (i.e. you
can use MAPIFolder.Folders(index) instead of MAPIFolder.Folders.Item(Index))
of the Folders collection, not MAPIFolder.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 

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