From: Tom P. on
I am trying to search an Outlook folder for certain e-mail items but I
am running into a problem getting items from the MAPIFolder object. I
use the following code but I can't find anything to cast the item to
that doesn't get an exception.


private List<SearchItem> SearchFolder(Outlook.MAPIFolder
CurrentFolder, string SearchString)
{
for (int iLoop = 1; iLoop < CurrentFolder.Items.Count;
iLoop++)
{
//This is where I get the exception
CurrentItem =
(Outlook.MailItemClass)CurrentFolder.Items[iLoop];
}
}

I've tried casting to MailItem, PostItem, Object, I've tried using
Folder.Items.GetFirst()... Nothing works. How do I get the item from
the MAPIFolder? Am I even supposed to be using the MAPIFolder?

I always get an exception: "Unable to cast COM object of type
'System.__ComObject' ........ failed due to the following error: No
such interface supported (Exception from HRESULT: 0x80004002
(E_NOINTERFACE))."

I've looked up several code examples and none of them address this.
I'm using Microsoft.Office.Interop.Outlook v 11.0

Any help would be appreciated.
Tom P.
From: Karl Mitschke on
Hello Tom P.,

> I am trying to search an Outlook folder for certain e-mail items but I
> am running into a problem getting items from the MAPIFolder object. I
> use the following code but I can't find anything to cast the item to
> that doesn't get an exception.
>
> private List<SearchItem> SearchFolder(Outlook.MAPIFolder
> CurrentFolder, string SearchString)
> {
> for (int iLoop = 1; iLoop < CurrentFolder.Items.Count;
> iLoop++)
> {
> //This is where I get the exception
> CurrentItem =
> (Outlook.MailItemClass)CurrentFolder.Items[iLoop];
> }
> }
> I've tried casting to MailItem, PostItem, Object, I've tried using
> Folder.Items.GetFirst()... Nothing works. How do I get the item from
> the MAPIFolder? Am I even supposed to be using the MAPIFolder?
>
> I always get an exception: "Unable to cast COM object of type
> 'System.__ComObject' ........ failed due to the following error: No
> such interface supported (Exception from HRESULT: 0x80004002
> (E_NOINTERFACE))."
>
> I've looked up several code examples and none of them address this.
> I'm using Microsoft.Office.Interop.Outlook v 11.0
>
> Any help would be appreciated.
> Tom P.

Is your mailbox on Exchnage?

If so, is it Exchange 2007 or 2010?

If so, Have you looked in to the Exchange Web Services Managed API?

If so, can I stop saying if so ?;)

If you are using a POP3 mailbox, see here:
http://support.microsoft.com/kb/310244

Also, for the Exchange Web Services, and other development issues, see here:
http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/threads

Karl
http://unlockpowershell.wordpress.com/


From: Tom P. on
On May 11, 11:41 am, Karl Mitschke <karlmitsc...(a)somestate.gov> wrote:
> Hello Tom P.,
>
>
>
> > I am trying to search an Outlook folder for certain e-mail items but I
> > am running into a problem getting items from the MAPIFolder object. I
> > use the following code but I can't find anything to cast the item to
> > that doesn't get an exception.
>
> > private List<SearchItem> SearchFolder(Outlook.MAPIFolder
> > CurrentFolder, string SearchString)
> > {
> > for (int iLoop = 1; iLoop < CurrentFolder.Items.Count;
> > iLoop++)
> > {
> > //This is where I get the exception
> > CurrentItem =
> > (Outlook.MailItemClass)CurrentFolder.Items[iLoop];
> > }
> > }
> > I've tried casting to MailItem, PostItem, Object, I've tried using
> > Folder.Items.GetFirst()... Nothing works. How do I get the item from
> > the MAPIFolder? Am I even supposed to be using the MAPIFolder?
>
> > I always get an exception: "Unable to cast COM object of type
> > 'System.__ComObject' ........  failed due to the following error: No
> > such interface supported (Exception from HRESULT: 0x80004002
> > (E_NOINTERFACE))."
>
> > I've looked up several code examples and none of them address this.
> > I'm using Microsoft.Office.Interop.Outlook v 11.0
>
> > Any help would be appreciated.
> > Tom P.
>
> Is your mailbox on Exchnage?

I was not aware this was a factor. I have no idea.

>
> If so, is it Exchange 2007 or 2010?
>
> If so, Have you looked in to the Exchange Web Services Managed API?

No, I have not. I was under the impression that Outlook and it's
objects would provide the interface to Exchange. I thought that's what
they were.

>
> If so, can I stop saying if so ?;)
>
> If you are using a POP3 mailbox, see here:http://support.microsoft.com/kb/310244

This is a perfect example of my issue. I go to this link and look-up
the samples. One says to use the following snippet to loop through
items:

Outlook.MAPIFolder oContacts =
olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
Outlook.Items oItems = oContacts.Items;
for (int i = 1; i <= oItems.Count; i++)
{
Outlook._ContactItem oContact = (Outlook._ContactItem)
oItems.Item(i);
Console.WriteLine(oContact.FullName);
oContact = null;
}


BUT when I declare an Outlook.Items variable (FolderItems) and then
try to use FolderItems.Item(iLoop) the compiler says there is no such
method.
Does it matter that I'm trying to get folders that are not Default
Folders? What if I want to look through the Personal Folders?

>
> Also, for the Exchange Web Services, and other development issues, see here:http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopme...
I have not looked here yet, but I will.

Tom P.
From: Tom P. on
On May 11, 12:13 pm, "Tom P." <padilla.he...(a)gmail.com> wrote:
> On May 11, 11:41 am, Karl Mitschke <karlmitsc...(a)somestate.gov> wrote:
>
>
>
> > Hello Tom P.,
>
> > > I am trying to search an Outlook folder for certain e-mail items but I
> > > am running into a problem getting items from the MAPIFolder object. I
> > > use the following code but I can't find anything to cast the item to
> > > that doesn't get an exception.
>
> > > private List<SearchItem> SearchFolder(Outlook.MAPIFolder
> > > CurrentFolder, string SearchString)
> > > {
> > > for (int iLoop = 1; iLoop < CurrentFolder.Items.Count;
> > > iLoop++)
> > > {
> > > //This is where I get the exception
> > > CurrentItem =
> > > (Outlook.MailItemClass)CurrentFolder.Items[iLoop];
> > > }
> > > }
> > > I've tried casting to MailItem, PostItem, Object, I've tried using
> > > Folder.Items.GetFirst()... Nothing works. How do I get the item from
> > > the MAPIFolder? Am I even supposed to be using the MAPIFolder?
>
> > > I always get an exception: "Unable to cast COM object of type
> > > 'System.__ComObject' ........  failed due to the following error: No
> > > such interface supported (Exception from HRESULT: 0x80004002
> > > (E_NOINTERFACE))."
>
> > > I've looked up several code examples and none of them address this.
> > > I'm using Microsoft.Office.Interop.Outlook v 11.0
>
> > > Any help would be appreciated.
> > > Tom P.
>
> > Is your mailbox on Exchnage?
>
> I was not aware this was a factor. I have no idea.
>
>
>
> > If so, is it Exchange 2007 or 2010?
>
> > If so, Have you looked in to the Exchange Web Services Managed API?
>
> No, I have not. I was under the impression that Outlook and it's
> objects would provide the interface to Exchange. I thought that's what
> they were.
>
>
>
> > If so, can I stop saying if so ?;)
>
> > If you are using a POP3 mailbox, see here:http://support.microsoft.com/kb/310244
>
> This is a perfect example of my issue. I go to this link and look-up
> the samples. One says to use the following snippet to loop through
> items:
>
> Outlook.MAPIFolder oContacts =
> olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
> Outlook.Items oItems = oContacts.Items;
> for (int i = 1; i <= oItems.Count; i++)
> {
>      Outlook._ContactItem oContact = (Outlook._ContactItem)
> oItems.Item(i);
>      Console.WriteLine(oContact.FullName);
>      oContact = null;
>
> }
>
> BUT when I declare an Outlook.Items variable (FolderItems) and then
> try to use FolderItems.Item(iLoop) the compiler says there is no such
> method.
> Does it matter that I'm trying to get folders that are not Default
> Folders? What if I want to look through the Personal Folders?
>

The problem is with Personal Folders. If I don't use
GetDefaultFolders() then none of the items will cast to valid
MailItems.

So, how do I loop through Non-default folders?

Tom P.


From: Mel Weaver on
Try

Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
oNS.Logon(Missing.Value, Missing.Value, false, true);

Outlook.MAPIFolder oBox = inbox == true ?
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) :
oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderSentMail);

Outlook.Items oItems = oBox.Items;
Outlook.MailItem oMsg;
Object outlookObject;

for (int i = 1; i <= oItems.Count; i++)
{
outlookObject = i == 1 ? oItems.GetFirst() : oItems.GetNext();

oMsg = outlookObject as Outlook.MailItem;
if (oMsg != null)
{
// do whatever you need with oMsg
}

}




"Tom P." <padilla.henry(a)gmail.com> wrote in message
news:7199159b-3cc0-46e0-94fa-9984490adae1(a)i10g2000yqh.googlegroups.com...
> On May 11, 11:41 am, Karl Mitschke <karlmitsc...(a)somestate.gov> wrote:
>> Hello Tom P.,
>>
>>
>>
>> > I am trying to search an Outlook folder for certain e-mail items but I
>> > am running into a problem getting items from the MAPIFolder object. I
>> > use the following code but I can't find anything to cast the item to
>> > that doesn't get an exception.
>>
>> > private List<SearchItem> SearchFolder(Outlook.MAPIFolder
>> > CurrentFolder, string SearchString)
>> > {
>> > for (int iLoop = 1; iLoop < CurrentFolder.Items.Count;
>> > iLoop++)
>> > {
>> > //This is where I get the exception
>> > CurrentItem =
>> > (Outlook.MailItemClass)CurrentFolder.Items[iLoop];
>> > }
>> > }
>> > I've tried casting to MailItem, PostItem, Object, I've tried using
>> > Folder.Items.GetFirst()... Nothing works. How do I get the item from
>> > the MAPIFolder? Am I even supposed to be using the MAPIFolder?
>>
>> > I always get an exception: "Unable to cast COM object of type
>> > 'System.__ComObject' ........ failed due to the following error: No
>> > such interface supported (Exception from HRESULT: 0x80004002
>> > (E_NOINTERFACE))."
>>
>> > I've looked up several code examples and none of them address this.
>> > I'm using Microsoft.Office.Interop.Outlook v 11.0
>>
>> > Any help would be appreciated.
>> > Tom P.
>>
>> Is your mailbox on Exchnage?
>
> I was not aware this was a factor. I have no idea.
>
>>
>> If so, is it Exchange 2007 or 2010?
>>
>> If so, Have you looked in to the Exchange Web Services Managed API?
>
> No, I have not. I was under the impression that Outlook and it's
> objects would provide the interface to Exchange. I thought that's what
> they were.
>
>>
>> If so, can I stop saying if so ?;)
>>
>> If you are using a POP3 mailbox, see
>> here:http://support.microsoft.com/kb/310244
>
> This is a perfect example of my issue. I go to this link and look-up
> the samples. One says to use the following snippet to loop through
> items:
>
> Outlook.MAPIFolder oContacts =
> olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
> Outlook.Items oItems = oContacts.Items;
> for (int i = 1; i <= oItems.Count; i++)
> {
> Outlook._ContactItem oContact = (Outlook._ContactItem)
> oItems.Item(i);
> Console.WriteLine(oContact.FullName);
> oContact = null;
> }
>
>
> BUT when I declare an Outlook.Items variable (FolderItems) and then
> try to use FolderItems.Item(iLoop) the compiler says there is no such
> method.
> Does it matter that I'm trying to get folders that are not Default
> Folders? What if I want to look through the Personal Folders?
>
>>
>> Also, for the Exchange Web Services, and other development issues, see
>> here:http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopme...
> I have not looked here yet, but I will.
>
> Tom P.