Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ComException when working with outlook 2003 1

Status
Not open for further replies.

koresnordic

IS-IT--Management
Nov 28, 2002
422
GB
HI,

Before I start, I am still learning VB (started two months ago as a sideline in my main job) and the routine below has been cobbled together through various guesses and parts pulled from here, there and everywhere.

The basis of the routine is to go through my inbox, find the unread messages that meet specific criteria, perform an action, mark the mail as read and move it to a subfolder of the inbox. The routine works fine if there is only one mail that matches the criteria. However if there is more than one, I get an error.

The routine is:

Imports System.Reflection
Imports Outlook = Microsoft.Office.Interop.Outlook

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Create Outlook application.
Dim oApp As Outlook.Application = New Outlook.Application
' String used for comparison with mail item.
Dim sClassComp = "IPM.Note"

' Get Mapi NameSpace.
Dim oNS As Outlook.NameSpace = oApp.GetNamespace("MAPI")

' Get Messages collection of Inbox.
Dim oInbox As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)
Dim oItems As Outlook.Items = oInbox.Items

Dim subfolder As Outlook.MAPIFolder
subfolder = oInbox.Folders("Auto_respond")


' Get unread e-mail messages.
oItems = oItems.Restrict("[Unread] = true")

' Loop each unread message.
Dim oMsg As Outlook.MailItem
Dim check_wanted As Integer
Dim Check_Store As Integer
Dim check_Till As Integer
Dim store As String
Dim till As String
Dim i As Integer
Dim ip_check As String
Dim looop As Integer

looop = oItems.Count
For i = 1 To looop

'Test to make sure item is a mail item
'and not a meeting request.
If oItems.Item(i).MessageClass = sClassComp Then
oMsg = oItems.Item(i)
' Ensure the message is one we want to work with
check_wanted = InStr(1, LCase$(oMsg.Body), "chip ", vbTextCompare) * InStr(1, LCase$(oMsg.Body), " pin", vbTextCompare)
Check_Store = InStr(1, oMsg.Subject, "## from ", vbTextCompare)
check_Till = InStr(1, oMsg.Body, "Till No : ", vbTextCompare)

If check_wanted <> 0 And Check_Store <> 0 Then

[perform routine]
' Mark mail as read
oMsg.UnRead = False
' Move to folder
oMsg.Move(subfolder)
End If
End If
Next

' Clean up.
oApp = Nothing
oNS = Nothing
oItems = Nothing
oMsg = Nothing

Me.Close()

End Sub


When there is more than one mail that matches, I get "ComException was unhandled - array index was out of bounds". This is on the first IF statement in the for..next loop

I am guessing it is because I have moved a message and there are no longer the same number of messages in the inbox as before, so it when I try to use "2" as the index, there are now only 1 relevant mail, so it fails, but I am not sure and have no idea how to go about dealing with it.

thanks for looking

[pc]

Graham
 
This is a problem with loops. It works great if the values remain constant. In your case, they don't.

You use
oItems.Count
to initialize the count for the loop.

Once you use
oMsg.Move(subfolder)
, you change the number of items so looop no longer has reference to the same values.

You will either need to change your loop structure, maybe do ... loop or save the subject of the message in an array and have a second loop at the end. If subject equal to _____ then delete message.

If at first you don't succeed, then sky diving wasn't meant for you!
 

Loop through the message in reverse order. That is, start at oItems.Count and decrement to 1. This way, when you remove an item it doesn't affect the items remaining to be processed and the loop can complete with no issues (related to the loop index, that is).

looop = oItems.Count
[red]For i = looop to 1 Step -1[/red]

'Test to make sure item is a mail item
'and not a meeting request.
If oItems.Item(i).MessageClass = sClassComp Then
oMsg = oItems.Item(i)
' Ensure the message is one we want to work with
check_wanted = InStr(1, LCase$(oMsg.Body), "chip ", vbTextCompare) * InStr(1, LCase$(oMsg.Body), " pin", vbTextCompare)
Check_Store = InStr(1, oMsg.Subject, "## from ", vbTextCompare)
check_Till = InStr(1, oMsg.Body, "Till No : ", vbTextCompare)

If check_wanted <> 0 And Check_Store <> 0 Then

[perform routine]
' Mark mail as read
oMsg.UnRead = False
' Move to folder
oMsg.Move(subfolder)
End If
End If
Next

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
This is a case where you have several options available to you.
You can also use a counter to help with the loop interations.
Code:
looop = oItems.Count        
Do While looop > 0
   If oItems.Item(i).MessageClass ...

   ...
   oMsg.Move(subfolder)
   looop = loop - 1
   ...
Loop

I would also suggest adding a Try ... Catch section to trap any error you might encounter.

If at first you don't succeed, then sky diving wasn't meant for you!
 

Also, I forgot to mention, most indexes in VB are zero-based. So, you might need to make the For/Next statement like this:

looop = oItems.Count - 1

For i = looop to 0 Step -1

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thanks for the help all. Bluejay07, the do loop worked fine. The procedure now looks like

looop = oItems.Count

Do While looop > 0


'Test to make sure item is a mail item
'and not a meeting request.
If oItems.Item(looop).MessageClass = sClassComp Then
oMsg = oItems.Item(looop)
.
.
.
.
End If
End If
looop = looop - 1
Loop


and it works almost perfectly. The only snag left is that when the mail gets moved to the subfolder, the date and time get reset to the time of the move, instead of keeping the original date & time. This is probbaly a symptom of outlook more than anything

[pc]

Graham
 
I'm glad you got it working.
Unfortunately I am not able to assist you with the date/time issue.

If at first you don't succeed, then sky diving wasn't meant for you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top