Get Email from Outlook Sub Folder

gc75150

New Member
Joined
Aug 1, 2021
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am fairly new to VBA programming. I need to access emails from an Outlook subfolder. When I run the procedure, I get a Run Time error '440' array index out of bounds. Could someone explain what this error means and the steps needed to correct my vba code? Thanks

Sub DownloadEmailAttachment()

Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olFolder As Outlook.MAPIfolder
Dim olItem As Object
Dim mailitem As Outlook.mailitem
Dim olAtt As Outlook.Attachment

Set olApp = New Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.Folders(olFolderInbox).Folders("CS Reports").Items


For Each olItem In olFolder.Items

If olItem.class = olMail Then
Set mailitem = olItem

Debug.Print mailitem.Subject
Debug.Print mailitem.ReceivedTime

End If

Next olItem

Set olFolder = Nothing
Set olNS = Nothing
Set olApp = Nothing


End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi. Can you tell us where the code broke? What line was it on when the error message came up? I don't have Outlook on my current computer (my desktop) to test it, but I suspect the problem was with the following line? I think you should change:
VBA Code:
Set olFolder = olNS.Folders(olFolderInbox).Folders("CS Reports").Items
to
VBA Code:
Set olFolder = olNS.GetDefaultFolder(olFolderInbox).Folders("CS Reports")

It's not the error message I would have expected, but that line seems like its problematic. Let me know if that fixes it for you.
 
Upvote 0
Hi. Can you tell us where the code broke? What line was it on when the error message came up? I don't have Outlook on my current computer (my desktop) to test it, but I suspect the problem was with the following line? I think you should change:
VBA Code:
Set olFolder = olNS.Folders(olFolderInbox).Folders("CS Reports").Items
to
VBA Code:
Set olFolder = olNS.GetDefaultFolder(olFolderInbox).Folders("CS Reports")

It's not the error message I would have expected, but that line seems like its problematic. Let me know if that fixes it for you.
Hi Dan. Yes, that line is where the code is breaking. I edited my code to the change you suggested and vba returned error message "Object could not be found." I changed the line of code to:

Set olFolder = olNS.Folders("username@domain.com").Folders("CS Reports")

Vba is now returning results from the "CS Reports" subfolder. When is it appropriate to use the email address instead of using the olNS.GetDefaultFolder(olFolderInbox) method? Thanks for pointing me in the right direction.
 
Upvote 0
Ahh of course - I didn't even think to ask - do you have your Outlook with multiple email accounts? I'm guessing that the CS Reports folder was one in your primary email account?

In any event, I'm glad it's working for you now. The key problem in your code (or at least, as far as I could see) was that you were attempting to assign the object of mails items to a folder - the line of code I asked you to change had .Items at the end, whereas the variable you were attempting to assign that object to (olFolder) had been previously dimensioned as a MAPIFolder.

VBA Code:
Dim olFolder As Outlook.MAPIfolder
Dim olItem As Object
Dim mailitem As Outlook.mailitem

If you had've attempted to assign the object to a variable dimensioned as an Object (like you have with olItem above), that would've been fine ... for that line of code. You will have quickly encountered another error, however, in the next line of code where you attempt to set up a loop through the items in the folder. That would have meant you would be trying to iterate through the Folders("CS Reports").Items.Items (and that just isn't a thing...) Does that make sense? I'm worried I may not be explaining it properly.

As an aside, there is redundancy in your code as below:
If olItem.class = olMail Then
Set mailitem = olItem

Debug.Print mailitem.Subject
Debug.Print mailitem.ReceivedTime

End If
Your mailitem variable is redundant because you're already assigned it to the same object to olitem (through using the for each loop). You will accomplish the same result as you have here if you delete:
VBA Code:
Set mailitem = olItem
and then replace all references to ICODE]mailitem[/ICODE] with olitem.

Let me know re: my question at the start of this reply, or if you experience any other problems.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,046
Messages
6,122,854
Members
449,096
Latest member
Erald

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top