koresnordic
IS-IT--Management
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
Graham
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
Graham