By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,587 Members | 1,070 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,587 IT Pros & Developers. It's quick & easy.

Extracting email attachment in Outlook

P: n/a
I'm writing a function to look for particular emails in my Inbox and
when it finds one it copies the attachment to a folder and then deletes
the email. It spots the email by looking for certain text in the
subject line. Here's a code fragment:

Dim appOutlook As Outlook.Application
Dim mpiFolder As MAPIFolder
Dim msgMail As MailItem
Dim strAQSPath As String
Dim strSubject As String
Dim strZIP As String

Set appOutlook = CreateObject("Outlook.Application")
Set mpiFolder =
appOutlook.GetNamespace("MAPI").GetDefaultFolder(o lFolderInbox)

strAQSPath = "c:\test\"

For Each msgMail In mpiFolder.Items
If msgMail.Subject Like "Transfer from AQS" & "*" Then
strSubject = msgMail.Subject
strZIP = Right(strSubject, Len(strSubject) - 18) & ".zip"
msgMail.Attachments.Item(1).SaveAsFile Trim(strAQSPath &
strZIP)
msgMail.Delete
End If
Next

It works perfectly except for one thing - it doesn't find all the
emails. For example, if there are 6 in my Inbox and I run the code it
will find, say, 3. Then if I run it again it will find another 2, and
if I run it again it will find the last one.

Doesn't make any sense to me. Anyone have any clues please?

Thanks
Dave

Aug 8 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 8 Aug 2006 02:16:31 -0700, "Dave G @ K2"
<Da*************@gmail.comwrote:

That makes sense. When you delete an email, you effectively have
changed the For loop because there is now one fewer element.
Perhaps better to change the loop, using pseudo-code like this:
Do
Find item with particular subject
if not found then exit do
Save attachment of found item
Delete item
Loop

-Tom.
>I'm writing a function to look for particular emails in my Inbox and
when it finds one it copies the attachment to a folder and then deletes
the email. It spots the email by looking for certain text in the
subject line. Here's a code fragment:

Dim appOutlook As Outlook.Application
Dim mpiFolder As MAPIFolder
Dim msgMail As MailItem
Dim strAQSPath As String
Dim strSubject As String
Dim strZIP As String

Set appOutlook = CreateObject("Outlook.Application")
Set mpiFolder =
appOutlook.GetNamespace("MAPI").GetDefaultFolder( olFolderInbox)

strAQSPath = "c:\test\"

For Each msgMail In mpiFolder.Items
If msgMail.Subject Like "Transfer from AQS" & "*" Then
strSubject = msgMail.Subject
strZIP = Right(strSubject, Len(strSubject) - 18) & ".zip"
msgMail.Attachments.Item(1).SaveAsFile Trim(strAQSPath &
strZIP)
msgMail.Delete
End If
Next

It works perfectly except for one thing - it doesn't find all the
emails. For example, if there are 6 in my Inbox and I run the code it
will find, say, 3. Then if I run it again it will find another 2, and
if I run it again it will find the last one.

Doesn't make any sense to me. Anyone have any clues please?

Thanks
Dave
Aug 8 '06 #2

P: n/a

Thanks Tom - I changed the code according to your suggestion and it
works fine

dave

Aug 8 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.