-
My understanding is that there is no way to force garbage collection in VBA.
I ran your code and it worked for me without issue, but I only had 59 attachments in 113 emails. I believe you are looping through too many items. Limit the initial collection of items to emails with attachments:
[vba]Function Downloadattachments(strpath As String)
Dim olApp As Outlook.Application
Dim expl As Outlook.Explorer
Dim currentItems As Outlook.Items
Dim itemsWithAttachments As Outlook.Items
Dim myItem As Object
Dim msg As Outlook.MailItem
' only loop through items with attachments
Set olApp = Outlook.Application
Set expl = olApp.ActiveExplorer
Set currentItems = expl.CurrentFolder.Items
Set itemsWithAttachments = currentItems.Restrict("[Attachments] > 0")
AttachmentCount = 0
For Each myItem In itemsWithAttachments
If TypeName(myItem) = "MailItem" Then
Set msg = myItem
Call downloadmail(msg, strpath)
End If
Next
MsgBox ("downloaded " & AttachmentCount & " attachments from " & itemsWithAttachments.Count & " emails")
End Function[/vba]
As an added benefit, you no longer have to select the emails before running the code -- it will simply save all attachments from every message in the folder.
Also, you might want to check the type of attachment, because Outlook treats images in signatures as 'attachments'. If an email has no "real" attachments, but the sender has an image in their signature, it will be added to the filtered collection above. Note that this would make itemsWithAttachments.Count inaccurate.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules