Jay Williams
asked on
Unexpected error in Access VBA email handler loop
Trying to diagnose an "Array index out of bounds." error in the following code where the subject line is defined. The idea is to save the attachments to different locations, move the emails and do different stuff according to the email subject line. The Array error kind of throws me; I know what out of bounds is, but was not aware that an array was created. What am I missing?
Public Sub RunChecksRequests()
Dim myolApp As Outlook.Application
Dim myItems As Object
Dim oMail As Outlook.MailItem
Dim myNamespace As Outlook.NameSpace
Set myolApp = CreateObject("Outlook.Application")
Set myNamespace = myolApp.GetNamespace("MAPI")
Set myItems = myNamespace.Folders("XE_IPP").Folders("Inbox").Items
On Error GoTo notfoundFolder
With myolApp
For Each oMail In myItems
If TypeName(oMail) = "MailItem" Then
If oMail.Subject = "IPP Share Request" And LCase(Right(oMail.Attachments.Item(1).FileName, 5)) = ".xlsm" Then
oMail.Attachments.Item(1).SaveAsFile "G:\XE_ECMs\IPP Sharing Development\Requests\" & oMail.Attachments.Item(1).FileName
Set rqFolder = myNamespace.Folders("XE_IPP").Folders("Inbox").Folders("Requests")
oMail.Move rqFolder
GoTo RequestsTurnaround
End If
If oMail.Subject = "IPP Share Check" And LCase(Right(oMail.Attachments.Item(1).FileName, 5)) = ".xlsm" Then
oMail.Attachments.Item(1).SaveAsFile "G:\XE_ECMs\IPP Sharing Development\Checks\" & oMail.Attachments.Item(1).FileName
Set chkFolder = myNamespace.Folders("XE_IPP").Folders("Inbox").Folders("Checks")
oMail.Move chkFolder
GoTo ChecksTurnaround
End If
End If
Next
On Error GoTo 0
Exit Sub
End With
notfoundFolder:
MsgBox "Unable to process."
RequestsTurnaround:
Call RequestsTurnaround
Exit Sub
ChecksTurnaround:
Call ChecksTurnaround
Exit Sub
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ouch. I didn't write it, but I have to fix it--but in all fairness, I'm pretty sure that I wouldn't have done much better. At least I know I'm not the only amateur in the world. As you can see, I need a lot of guidance on this. When I go in tomorrow I'll start picking at it. Thanks, guys.
ASKER
Just thinking: Maybe we need a dynamic array that can handle any number of items? Maybe a Select Case strategy might be a better than deeply nested If statements?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Bing (the sound of lights coming on)! Now we'll see if I can do it. Hope you don't go on vacation! :-)
ASKER
Okay. I'm a Select Case virgin. Please be gentle.
When I try to compile this code I get an "End With without With" error.
How and when do you "'bail"? Pretty sure I'm not using End Select properly.
When I try to compile this code I get an "End With without With" error.
Public Sub RunChecksRequests()
Dim myolApp As Outlook.Application
Dim myItems As Object
Dim oMail As Outlook.MailItem
Dim myNamespace As Outlook.NameSpace
Set myolApp = CreateObject("Outlook.Application")
Set myNamespace = myolApp.GetNamespace("MAPI")
Set myItems = myNamespace.Folders("XE_IPP").Folders("Inbox").Items
On Error GoTo 0
With myolApp
For Each oMail In myItems
Select Case True
Case oMail.Attachments.Count = 0 'no attachments
Set plnFolder = myNamespace.Folders("XE_IPP").Folders("Inbox").Folders("Plain")
oMail.Move rqFolder
End Select
Select Case True
Case Nz(oMail.Subject = "") 'no subject
Set plnFolder = myNamespace.Folders("XE_IPP").Folders("Inbox").Folders("Plain")
oMail.Move plnFolder
End Select
Select Case True
Case oMail.Subject = "IPP Share Request"
'Block of code to test attachment
LCase(Right(oMail.Attachments.Item(1).FileName, 5)) = ".xlsm"
oMail.Attachments.Item(1).SaveAsFile "G:\XE_ECMs\IPP Sharing Development\Requests\" & oMail.Attachments.Item(1).FileName
Set rqFolder = myNamespace.Folders("XE_IPP").Folders("Inbox").Folders("Requests")
oMail.Move rqFolder
Call RequestsTurnaround
Case oMail.Subject = "IPP Share Check"
'Block of code to test attachments
LCase(Right(oMail.Attachments.Item(1).FileName, 5)) = ".xlsm"
oMail.Attachments.Item(1).SaveAsFile "G:\XE_ECMs\IPP Sharing Development\Checks\" & oMail.Attachments.Item(1).FileName
Set chkFolder = myNamespace.Folders("XE_IPP").Folders("Inbox").Folders("Checks")
oMail.Move chkFolder
Call ChecksTurnaround
End Select
End With
End Sub
I realize there is probably other stuff wrong with this code, too (and I'm hoping you're kind enough to point it out), but for now, I clearly have some basic understanding missing.How and when do you "'bail"? Pretty sure I'm not using End Select properly.
You are missing the Next for your For loop. Insert between lines 41 and 42.
ASKER
Thanks, Shaun. Does the other stuff make sense to you?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Good! Just the kind of help I need. Thanks again.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@Dale
Bad aircode on my part, good catch.
Select Case True operates very similarly to if elseif -- But nesting ifs can get very confusing in a hurry. Select Case True lets you build more readable code when you need IF in the blocks of execution
Bad aircode on my part, good catch.
Select Case True operates very similarly to if elseif -- But nesting ifs can get very confusing in a hurry. Select Case True lets you build more readable code when you need IF in the blocks of execution
ASKER
This really was collaborative, and you all got me within striking distance. I have some other related issues, but they will go on another thread if they prove too stubborn. Thanks all.
If Subject = ... Then
If Attachments.Count > 0 Then
If Attachments.Item(1).FileNa