alcibiades
New member
- Local time
- Yesterday, 23:06
- Joined
- Jul 27, 2010
- Messages
- 4
I cribbed the following code from someplace off this forum & modified it to save attachments from e-mails with a specific subject line. The problem I'm having is that the public inbox that code is scanning contains thousands of e-mails (for business reasons they are not deleted) that this script has to run through before it completes.
I found that having it look through Unread items is pretty fast, but ideally I would like it to look for anything received current day which is not very fast...I provided both lines of code below (in red)
Is there a way that I can code it either to stop when it finds its first hit or only consider current day e-mails prior to looking at each one individually?
Thanks in advance for your help
Sub SaveAttachments()
Dim myOlapp As Outlook.Application
Dim myNameSpace As Outlook.Namespace
Dim myFolder As Outlook.MAPIFolder
Dim myItem As Outlook.MailItem
Dim myAttachment As Outlook.Attachment
Set myOlapp = CreateObject("Outlook.Application")
Set myNameSpace = myOlapp.GetNamespace("MAPI")
Set myFolder = myNameSpace.Folders("Mailbox - Shared").Folders("Inbox")
For Each myItem In myFolder.Items
'Looks at unread e-mails - fast but not ideal
If InStr(myItem.Subject, "Subject") And myItem.UnRead Then
'Looks at e-mails received today - ideal but slow
If InStr(myItem.Subject, "Subject") And myItem.SentOn > Date Then
For Each myAttachment In myItem.Attachments
myAttachment.SaveAsFile "Y:\OutlookTest.xls"
Next
End If
Next
End Sub
I found that having it look through Unread items is pretty fast, but ideally I would like it to look for anything received current day which is not very fast...I provided both lines of code below (in red)
Is there a way that I can code it either to stop when it finds its first hit or only consider current day e-mails prior to looking at each one individually?
Thanks in advance for your help
Sub SaveAttachments()
Dim myOlapp As Outlook.Application
Dim myNameSpace As Outlook.Namespace
Dim myFolder As Outlook.MAPIFolder
Dim myItem As Outlook.MailItem
Dim myAttachment As Outlook.Attachment
Set myOlapp = CreateObject("Outlook.Application")
Set myNameSpace = myOlapp.GetNamespace("MAPI")
Set myFolder = myNameSpace.Folders("Mailbox - Shared").Folders("Inbox")
For Each myItem In myFolder.Items
'Looks at unread e-mails - fast but not ideal
If InStr(myItem.Subject, "Subject") And myItem.UnRead Then
'Looks at e-mails received today - ideal but slow
If InStr(myItem.Subject, "Subject") And myItem.SentOn > Date Then
For Each myAttachment In myItem.Attachments
myAttachment.SaveAsFile "Y:\OutlookTest.xls"
Next
End If
Next
End Sub