Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook email attachments saved to hard drive MACRO not working

Status
Not open for further replies.

Queryman

Programmer
Nov 4, 2002
243
US
I have code that pulls all the attachments from a subfolder in my Outlook inbox and saves it to my hard drive, works great, but only seems to pull the oldest file in cases where there are duplicate file sent by the same person. Does anyone know using the following macro how to pull just the latest file from Outlook in case of duplicates, here is the code. Also if someone knows how to pull from another outlook mailbox that I have full access to (not my own), I'd appreciate that too.

Sub ExtractFiles()
Dim oApp As Application
Dim myFolder As Outlook.MAPIFolder
Dim oFolder As Outlook.MAPIFolder
Dim oMsg As MailItem
Dim oNS As NameSpace
Dim oAttachments As Outlook.Attachments
Dim oAttachment As Object
Dim oObject As Object
Dim oSubAttachment As Object
Dim oItem As Items 'use as subattachment in this case
Dim strControl

Set oApp = New Outlook.Application
Set oNS = oApp.GetNamespace("MAPI")
Set oFolder = oNS.GetDefaultFolder(olFolderInbox)
Set myFolder = oFolder.Folders("TEMP")
strControl = ""

For Each oMsg In myFolder.Items 'oMsg is an item
With oMsg 'oMsg has attachment(s)
If (oMsg.Attachments.Count > 0) Then
strControl = Day(oMsg.ReceivedTime) & Month(oMsg.ReceivedTime) & Year(oMsg.ReceivedTime)
For Each oAttachment In oMsg.Attachments
With oAttachment
oAttachment.SaveAsFile "C:\TEMP\" & strControl & "-" & oAttachment.FileName
End With
Next
Else
' The next bit looks up the sender name from the
' address book and may well fall foul of security
'
' MsgBox "No attachments on email from " & oMsg.SenderName
End If
End With
Next

End Sub




Michael

 
Have you tried to replace this:
For Each oMsg In myFolder.Items
With something like this ?
For i = myFolder.Items.Count - 1 To 0 Step -1
Set oMsg = myFolder.Items(i)

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Sorry, I tried that just now and it still saves only the oldest submission, not the newest.

I have 16 items in taht folder, with only nine uniques, all teh rest are duplicate submissions. The only difference in the emails is the subject and the attachment.




Michael

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top