I have the following code that saves any attachment to a folder.
I am having trouble getting it to then delete the original email
I tried using Item.Delete but it doesn't get them all
Any help would be appreciated
Private Sub Application_Startup() ' This Outlook macro checks a the Outlook Inbox for messages ' with attached files (of any type) and saves them to disk. ' NOTE: make sure the specified save folder exists before ' running the macro. This code requires a reference to be set ' to the Microsoft Outlook 8.0 Object Model On Error GoTo GetAttachments_err ' Declare variables Dim appOl As New Outlook.Application Dim ns As Outlook.NameSpace Dim Inbox As Outlook.MAPIFolder Dim Item As Object Dim Atmt As Outlook.Attachment Dim FileName As String Dim i As Integer Set ns = appOl.GetNamespace("MAPI") Set Inbox = ns.GetDefaultFolder(olFolderInbox) i = 0 ' Check Inbox for messages and exit of none found If Inbox.Items.Count = 0 Then MsgBox "There are no messages in the Inbox.", vbInformation, _ "Nothing Found" Exit Sub End If ' Check each message for attachments For Each Item In Inbox.Items ' Save any attachments found For Each Atmt In Item.Attachments ' This path must exist! Change folder name as necessary. FileName = "C:\EmailAttachments\" & Atmt.FileName Atmt.SaveAsFile FileName i = i + 1 Next Atmt Next Item ' Show summary message If i > 0 Then MsgBox "I found " & i & " attached files." _ & vbCrLf & "I have saved them into the C:\Email Attachments folder." _ & vbCrLf & vbCrLf & "Have a nice day.", vbInformation, "Finished!" Else MsgBox "I didn't find any attached files in your mail.", vbInformation, "Finished!" End If ' Clear memory Set Atmt = Nothing Set Item = Nothing Set ns = Nothing Exit Sub ' Handle errors GetAttachments_err: MsgBox "An unexpected error has occurred." _ & vbCrLf & "Please note and report the following information." _ & vbCrLf & "Macro Name: GetAttachments" _ & vbCrLf & "Error Number: " & Err.Number _ & vbCrLf & "Error Description: " & Err.Description _ , vbCritical, "Error!" End Sub
"May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."
Use code tags when posting your VBA code: [code] Your code here [/code]
Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.
Placing it before the "Next ATMT " line deleted all but the last 2 emails?
So not sure if it is a matter of where I am placing it.
I placed it After "Next Amnt" and it deleted up to an email with no attachment.
Do you want to delete all the emails or just ones with attachments?
Dom
"May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."
Use code tags when posting your VBA code: [code] Your code here [/code]
Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.
First .... Thank you for your responses
All of them AFTER I have saved the attachments
I would expect this to work:
For Each Item In Inbox.Items ' Save any attachments found For Each Atmt In Item.Attachments ' This path must exist! Change folder name as necessary. FileName = "C:\EmailAttachments\" & Atmt.FileName Atmt.SaveAsFile FileName i = i + 1 Next Atmt Item.Delete Next Item
Dom
"May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."
Use code tags when posting your VBA code: [code] Your code here [/code]
Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.
So would I have but it didn't delete them all.
Left the last 2 there (not sure if the number it left is significant)
OK ran it several times and always left the last 2.
With those 2 left as the only ones in my inbox....Ran it again and it deletes those 2??????
hmmmmm
I have to admit I've got some similar code I wrote to strip attachments from my sent items to save space at work and it occasionally misses one. I've never really bothered to work out the reason why as it's few and far between.
I'll have to test this tomorrow at work and see if I get a similar result.
Dom
"May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."
Use code tags when posting your VBA code: [code] Your code here [/code]
Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.
Seems to be random but happens every time I run it.
Just put 3 mails in the box left only the last one this time.
Not only does it not delete the mails but it doesn't save the attachment either.... Like the code decides there is the right place to stop
Isn't coding in Outlook fun
Dom
"May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."
Use code tags when posting your VBA code: [code] Your code here [/code]
Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.
First time trying it
is a bit of a piecework code but it works great until I try to delete the mails
Could I just add some code at the end of it to empty inbox?
Just doing a bit of research and it appears there can be some problems looping through a collection in Outlook like that. It seems that you are better looping through the items backwards.
Again I will have to have a play tomorrow as unable to now.
Dom
"May the fleas of a thousand camels infest the crotch of the person who screws up your day and may their arms be too short to scratch..."
Use code tags when posting your VBA code: [code] Your code here [/code]
Remember, saying thanks only takes a second or two. Click the little star to give some Rep if you think an answer deserves it.
Appreciate it very much.
I will check back tomorrow![]()
Any Luck with this?
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks