PDA

View Full Version : attachment download from outlook



samuelimtech
01-29-2014, 03:19 AM
Hi all,
I have the following code to download all files from an outlook folder and save it in a specified folder. The script runs through one script then generates a run time error(****) when it tries to save from the next email. can anyone see why?




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
Dim i As Long

Set myOlapp = CreateObject("Outlook.Application")
Set myNameSpace = myOlapp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myFolder = myFolder.Folders("Timesheets") 'Change this!!!!!!!!!!!!!!!!!

For Each myItem In myFolder.Items
If myItem.Attachments.count <> 0 Then
For Each myAttachment In myItem.Attachments
i = i + 1
**** myAttachment.SaveAsFile "C:\Users\samuel.brierley\Desktop\Timesheets (STSS)\Download\" & myItem.Attachments.Item(i).Filename
Next
End If

Next

End Sub

snb
01-29-2014, 03:47 AM
Sub M_snb()
For Each It In CreateObject("Outlook.Application").GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders("Timesheets").items
For Each at In It.Attachments
at.SaveAsFile "C:\Users\samuel.brierley\Desktop\Timesheets (STSS)\Download\" & at.Filename
Next
Next
End Sub

samuelimtech
01-29-2014, 04:23 AM
thank you thats got it.