+ Reply to Thread
Results 1 to 15 of 15

Thread: Having trouble deleting email from inbox

  1. #1
    Registered User
    Join Date
    07-19-2011
    Location
    Ohio
    MS-Off Ver
    Excel 2003
    Posts
    9

    Having trouble deleting email from inbox

    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

  2. #2
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Having trouble deleting email from inbox

    Quote Originally Posted by rwilfing View Post
    I tried using Item.Delete but it doesn't get them all
    Did it work at all? I don't have Outlook installed at home but would have expected that to work.

    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.

  3. #3
    Registered User
    Join Date
    07-19-2011
    Location
    Ohio
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Having trouble deleting email from inbox

    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.

  4. #4
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Having trouble deleting email from inbox

    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.

  5. #5
    Registered User
    Join Date
    07-19-2011
    Location
    Ohio
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Having trouble deleting email from inbox

    First .... Thank you for your responses


    All of them AFTER I have saved the attachments

  6. #6
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Having trouble deleting email from inbox

    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.

  7. #7
    Registered User
    Join Date
    07-19-2011
    Location
    Ohio
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Having trouble deleting email from inbox

    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)

  8. #8
    Registered User
    Join Date
    07-19-2011
    Location
    Ohio
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Having trouble deleting email from inbox

    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

  9. #9
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Having trouble deleting email from inbox

    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.

  10. #10
    Registered User
    Join Date
    07-19-2011
    Location
    Ohio
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Having trouble deleting email from inbox

    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

  11. #11
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Having trouble deleting email from inbox

    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.

  12. #12
    Registered User
    Join Date
    07-19-2011
    Location
    Ohio
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Having trouble deleting email from inbox

    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?

  13. #13
    Forum Guru Domski's Avatar
    Join Date
    12-14-2009
    MS-Off Ver
    What does it matter?
    Posts
    3,933

    Re: Having trouble deleting email from 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.

  14. #14
    Registered User
    Join Date
    07-19-2011
    Location
    Ohio
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Having trouble deleting email from inbox

    Appreciate it very much.

    I will check back tomorrow

  15. #15
    Registered User
    Join Date
    07-19-2011
    Location
    Ohio
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: Having trouble deleting email from inbox

    Any Luck with this?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0