nomb

Hey guys,

I haven't figured out yet how to make an outlook-addon so atm I am stuck doing a windows app. I have a timer which is fired every 45 seconds. It then cycles through whatever folder you choose and takes each mailitem and adds it to a list view. Then deletes the item. The only thing is that every time it ticks, it only goes through half. For example if you have 20 messages in the deleted items folder, it would do:

add/delete 10

add/delete 5

add/delete 3

add/delete 1

add/delete 1

Here is the code:

private void getMessages(Outlook.MAPIFolder oFolder)

{

Redemption.SafeMailItem rMail = new Redemption.SafeMailItem();

try

{

Outlook.Items oMessageItems = oFolder.Items;

foreach (Outlook.MailItem oMail in oMessageItems)

{

try

{

Redemption.SafeMailItem rMailItem = new Redemption.SafeMailItem();

rMailItem.Item = oMail;

ListViewItem item = new ListViewItem();

item.Text = "X";

item.SubItems.Add("");

item.SubItems.Add(rMailItem.SenderName);

item.SubItems.Add(rMailItem.SenderEmailAddress);

item.SubItems.Add(oMail.Subject);

item.SubItems.Add(rMailItem.Body);

listView1.Invoke(new UpdateListViewDelegate(this.UpdateListView), new object[] { item });

oMail.Delete();

}

catch (Exception ex)

{

//MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);

}

}

oMessageItems = null;

}

catch (Exception ex)

{

MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);

}

finally

{

this.listView1.EndUpdate();

rMail = null;

}

}

Also, if it hits something other then a mailmessage, like a meeting request mail it errors. How can I test for this and keep the process going

Thanks



Re: Visual Studio Tools for Office Windows app to monitor outlook folder cycles through only half each tick?

Sue Mosher - Outlook MVP

Never move or delete items inside a foreach loop. Instead, you can use a loop that counts down from oMessageItems.Count to 1, using the oMessageItems.Item method to return each individual item.

If you return the item as an Object instead of a MailItem, you then can use the Is operator to check the item type, e.g.:

if (Item is Outlook.MailItem)





Re: Visual Studio Tools for Office Windows app to monitor outlook folder cycles through only half each tick?

nomb

Sue Mosher - Outlook MVP wrote:

Never move or delete items inside a foreach loop. Instead, you can use a loop that counts down from oMessageItems.Count to 1, using the oMessageItems.Item method to return each individual item.

If you return the item as an Object instead of a MailItem, you then can use the Is operator to check the item type, e.g.:

if (Item is Outlook.MailItem)

I'm trying to do this as you said, however I don't have a oMessageItems.Item.

I might as well post this too this is how I'm starting off:

while (!(oMessageItems.Count == 0))

{

if (oMessageItems. <-- gonna check type here and if it is a mail item add it to the listview and delete it.

}

Is this right

Nate





Re: Visual Studio Tools for Office Windows app to monitor outlook folder cycles through only half each tick?

Sue Mosher - Outlook MVP

oMessageItems is your Items collection. It is each individual item whose type you need to check (or ignore the error).

This article shows better how to return an individual item, given its index -- http://support.microsoft.com/ kbid=310244. Look under the "Referencing existing items" section. A while loop would work, but probably be less performant than a down-counting for loop, because it would need to return the count for each pass through the loop.





Re: Visual Studio Tools for Office Windows app to monitor outlook folder cycles through only half each tick?

nomb

This isn't the exact code I'm going to use but this is exactly what is in the example aside from using a folder as the source:

Outlook.Items oMessageItems = oFolder.Items;

for (int i = 1; i <= oMessageItems.Count; i++)

{

Outlook._MailItem oContact = (Outlook._MailItem)oMessageItems.Item(i); <-- errors here because .Item isn't there

}

This is the error it gives:

'Microsoft.Office.Interop.Outlook.Items' does not contain a definition for 'Item'.

Am I using it the wrong way





Re: Visual Studio Tools for Office Windows app to monitor outlook folder cycles through only half each tick?

Sue Mosher - Outlook MVP

Alas, I know next to nothing about troubleshooting C# code. Do you have the right PIA installed and referenced in your project





Re: Visual Studio Tools for Office Windows app to monitor outlook folder cycles through only half each tick?

nomb

I believe I do. I posted the msdn code directly and got the very same error saying .Item wasn't there...

This is getting frustrating... Big Smile I guess I should as what PIA stands for. Is that the com object

Nate





Re: Visual Studio Tools for Office Windows app to monitor outlook folder cycles through only half each tick?

Sue Mosher - Outlook MVP

PIA = primary interop assembly. You need to install it from the Office CD or from the download before writing any Office code that uses COM Interop. Check your reference to make sure it's referring to the copy in the GAC. If not, then you are using the wrong reference and may need to install the PIAs.



Re: Visual Studio Tools for Office Windows app to monitor outlook folder cycles through only half each tick?

nomb

The Outlook Interop Assembly points to: C:\WINDOWS\assembly\GAC\Microsoft.Office.Interop.Outlook\11.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Outlook.dll

The Office Core points to: C:\WINDOWS\assembly\GAC\Office\11.0.0.0__71e9bce111e9429c\Office.dll





Re: Visual Studio Tools for Office Windows app to monitor outlook folder cycles through only half each tick?

nomb

Well I found someone else having the same problem and he used .getnext() instead. However, I am still having the same problem where it only deletes half of the mailbox at a time. This is the code I have right now:

try

{

Outlook.Items oMessageItems = oFolder.Items;

for (int i = 0; i <= oMessageItems.Count; i++)

{

Outlook._MailItem oMessage = (Outlook._MailItem)oMessageItems.GetNext();

Redemption.SafeMailItem rMailItem = new Redemption.SafeMailItem();

rMailItem.Item = oMessage;

ListViewItem item = new ListViewItem();

item.Text = "X";

item.SubItems.Add("");

item.SubItems.Add(rMailItem.SenderName);

item.SubItems.Add(rMailItem.SenderEmailAddress);

item.SubItems.Add(oMessage.Subject);

item.SubItems.Add(rMailItem.Body);

listView1.Invoke(new UpdateListViewDelegate(this.UpdateListView), new object[] { item });

oMessage.Delete();

}

oMessageItems = null;

}

catch (Exception ex)

{

//MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);

}

finally

{

this.listView1.EndUpdate();

}





Re: Visual Studio Tools for Office Windows app to monitor outlook folder cycles through only half each tick?

Sue Mosher - Outlook MVP

GetNext is going to have the same problem, because the indices changes every time you delete or move an item from the collection. Using GetFirst repeatedly might work, though, since you'd be deleting the first item every time.

Again, showing my C# ignorance, does this expression work:

oMessageItems[i]





Re: Visual Studio Tools for Office Windows app to monitor outlook folder cycles through only half each tick?

nomb

Well I found the problem to the half return error. Basically since I was deleting one everytime I went to the next mail, the way both loops work, if i = oMessageItems.Count then the loop is over. So what was happening was if you had 100 emails, when you are viewing your 50th email it also means you deleted 50 of them. So therefore i == oMessageItems and the loop would halt thining it was done, even though it only went through half. Now I need to figure out how I can delete them...