0
votes

This is the problem:

I am using C# with the Interop.Outlook library. I can get to the shared folder which is where the emails are located that I need to scan and grab values to input into a delimited text file to be transferred into a spreadsheet.

However currently the program only scans the emails in the parent level shared inbox folder. Most of the emails that I need are in this folder but there are a few sub folders within inbox that I need to scan that I cannot get to at this time.

Here is the current code:

 using Microsoft.Office.Interop;


 Outlook.Application app = new Outlook.Application();
 Outlook._NameSpace nSpace = app.GetNamespace("MAPI");
 Outlook.Recipient recip = nSpace.CreateRecipient("sharedFolderName");
 recip.Resolve();

 Outlook.MAPIFolder theParent = 
     nSpace.GetSharedDefaultFolder(recip, OlDefaultFolders.olFolderInbox).Parent;
 Outlook.MAPIFolder shared = 
     nSpace.GetSharedDefaultFolder(recip, OlDefaultFolders.olFolderInbox);
 Outlook.MAPIFolder subFolder = 
     theParent.Folders["Inbox"].Folders["FolderNameINeedToScan"];

I think my problem is on the last line I have tried to remove the second .Folders but that just gets the parent inbox. The current error that is returned is "An Object could not be found." The folder that I am looking for does exist.

How can I get to the child folders in Outlook?

2
My first recommendation to people trying to program Outlook is to install OutlookSpy dimastr.com/outspy/home.htm Gives you all sorts of interesting information. - RenniePet
@RenniePet Thanks but I cannot install this software into Outlook since this is for work and I do not have authorization to add this type of software to a shared drive. - IModulo5

2 Answers

2
votes

You cannot get child or parent folders of a delegate folder obtained by GetSharedDefaultFolder. You would need to have that user granted Full Mailbox access on the required Exchange Mailbox and then add that Mailbox to the current Outlook profile. All the folders in that Mailbox will then be available in the NameSpace.Stores collection.

If you cannot add another Mailbox to the Outlook profile, then you can use Redemption to logon to that Mailbox and access the folders while Outlook is open. Otherwise, you can use NameSpace.Logon when Outlook is closed to logon to a specific Mailbox.

-2
votes

This was solved by using the hints that Eric gave me in his answer.

I had to change which nameSpace I was using. Originally I used

Outlook.Application app = new Outlook.Application();
Outlook._NameSpace nSpace = app.GetNamespace("MAPI");
Outlook.Recipient recip = nSpace.CreateRecipient("name of recipient");

Then I would use the recipient to create an access to the shared inbox. However I was unable to get to the sub folders in that inbox. Thanks to Eric I switched to this:

Outlook._NameSpace nSpace = app.GetNamespace("MAPI");
Outlook.Stores theStore = nSpace.Stores;
Folders subFolder = recip.Parent.Folders;    
dynamic email;
for(int i = 1; i<= subFolder.GetFirst().Folders["folderName"].Folders["subFolderName"].Items.Count; i++)
{
    email = subFolder.GetFirst().Folders["folderName].Folders["subFolderName"].Items[i];

The final line is the magic cast to get into the sub folder and scan through all the emails. Thanks Eric