Consulting

Results 1 to 12 of 12

Thread: Delete Sub-folder Contents

  1. #1

    Delete Sub-folder Contents

    I am using the following code which deletes the contents of the Deleted items folder. it works perfectly.
    Public Sub EmptyDeletedEmailFolder()
    
    
        Dim outApp As Outlook.Application
        Dim deletedFolder As Outlook.MAPIFolder
        Dim item As Object
        Dim entryID As String
    
    
        Set outApp = CreateObject("outlook.application")
        Set deletedFolder = outApp.GetNamespace("MAPI").GetDefaultFolder(olFolderDeletedItems)
    
    
        For i = deletedFolder.Items.Count To 1 Step -1
        deletedFolder.Items(i).Delete             '' Delete from mail folder
        Next
    
    
        Set item = Nothing
        Set deletedFolder = Nothing
        Set outApp = Nothing
    
    
    End Sub
    When I try to amend the code to delete a sub folder called MyFolder it keeps throwing a runtime -2147024809 (80070057) error. I have indicated the line that I changed in the code below. Any guidance would be appreciated.

    Public Sub EmptyDeletedEmailFolder()
    
    
        Dim outApp As Outlook.Application
        Dim deletedFolder As Outlook.MAPIFolder
        Dim item As Object
        Dim entryID As String
    
    
        Set outApp = CreateObject("outlook.application")
       
    
        'I changed olFolderDeletedItems to olFolderMyFolder at the end of the next line
        Set deletedFolder = outApp.GetNamespace("MAPI").GetDefaultFolder(olFolderMyFolder)
    
    
        For i = deletedFolder.Items.Count To 1 Step -1
        deletedFolder.Items(i).Delete             '' Delete from mail folder
        Next
    
    
        Set item = Nothing
        Set deletedFolder = Nothing
        Set outApp = Nothing
    
    
    End Sub
    Thanks

  2. #2
    My own folder deletion macro is as follows. You can modify it to suit your requirements.
    Sub RemoveEmptyFolders()
    Dim i As Long, j As Long
    Dim iFolder As Folder
    Dim subFolder As Folder
    Dim Ask As String
        For i = Session.GetDefaultFolder(olFolderInbox).folders.Count To 1 Step -1
            Set iFolder = Session.GetDefaultFolder(olFolderInbox).folders(i)
            For j = iFolder.folders.Count To 1 Step -1
                Set subFolder = iFolder.folders(j)
                If subFolder.Items.Count = 0 And _
                   subFolder.folders.Count = 0 Then
                    Ask = MsgBox(iFolder.Name & " > " & subFolder.Name, vbYesNo, "Delete sub folder")
                    If Ask = vbYes Then
                        subFolder.Delete
                    End If
                End If
            Next j
            If iFolder.Items.Count = 0 And _
               iFolder.folders.Count = 0 Then
                Ask = MsgBox(iFolder.Name, vbYesNo, "Delete folder")
                If Ask = vbYes Then
                    iFolder.Delete
                End If
            End If
        Next i
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Hi Graham,

    Thanks for the response. My bad as I explained what I am trying to do poorly. I am trying to delete the contents of the sub folder as opposed to the sub folder itself. In the example I posted I thought a straight change from the "Deleted Items" folder to my target "MyFolder" would work but no luck.

    Cheers,

    Des

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    When your code drills down to the subfolder then something like

    i=subfoolder items count to 1 step -1 \
    delete itmes(i)
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    Hi Sam,

    Thanks for the response. The problem is the subfolder. When I change the "Deleted Items" folder as my target folder to my sub folder called "MyFolder" it throws the run time error. If I could just get that to work then I am sorted.

    Any ideas greatly appreciated.

    Thanks,

    Des

  6. #6
    The following will delete all the files from the deleted items folder and all its sub folders
    Sub DeleteMessages()
    Dim olNS As NameSpace
    Dim olfolder As Folder
    Dim subFolder As Folder
    Dim cFolders As New Collection
    Dim i As Long
        Set olNS = GetNamespace("MAPI")
        cFolders.Add olNS.GetDefaultFolder(olFolderDeletedItems)
        Do While cFolders.Count > 0
            Set olfolder = cFolders(1)
            cFolders.Remove 1
            For i = olfolder.Items.Count To 1 Step -1
                'delete the files
                olfolder.Items(i).Delete
            Next i
            For Each subFolder In olfolder.folders
                cFolders.Add subFolder
            Next subFolder
        Loop
        'Optional Delete the sub folders
        'For Each subFolder In olNS.GetDefaultFolder(olFolderDeletedItems).folders
         '   subFolder.Delete
        'Next
    CleanUp:
        Set olfolder = Nothing
        Set subFolder = Nothing
        Set cFolders = Nothing
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    Hi Graham,

    I am nearly there with this. Your solution works perfectly if I want to Delete the contents of the "Delete Items" Folder and all Subfolders contents. I don't wish to delete the "Delete Items" folder contents, also I don't wish to delete all sub folder content just the sub folder contents for the sub folder called "MyFolder".

    Thanks for your patience!

    Cheers,

    Des

  8. #8
    Hmmm. This is complicated by the fact that if you delete an item from a sub folder of the deleted items folder, the file is simply moved to the parent folder, so not only do you have to delete the item from the sub folder you have to delete it from the parent folder e.g.

    Option Explicit
    Sub EmptyMyFolder()
    Dim olMyFolder As Outlook.Folder
    Dim olDeletedFolder As Outlook.Folder
    Dim olItems As Outlook.Items
    Dim olItem As Object
    Dim olDelItem As Object
    Dim i As Long, j As Long
        On Error Resume Next
        Set olMyFolder = Session.GetDefaultFolder(olFolderDeletedItems).folders("MyFolder")
        Set olDeletedFolder = Session.GetDefaultFolder(olFolderDeletedItems)
        Set olItems = olMyFolder.Items
        For i = olItems.Count To 1 Step -1
            Set olItem = olItems(i)
            olItem.categories = "DEL"
            olItem.Save
            olItem.Delete
            DoEvents
            For j = olDeletedFolder.Items.Count To 1 Step -1
                Set olDelItem = olDeletedFolder.Items(j)
                If olDelItem.categories = "DEL" Then
                    olDelItem.Delete
                    Exit For
                End If
            Next j
        Next i
        Set olMyFolder = Nothing
        Set olDeletedFolder = Nothing
        Set olItems = Nothing
        Set olItem = Nothing
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  9. #9
    Hi Graham,

    I don't know if this helps or hinders but it's actually a sub folder of the Inbox that I am deleting the contents of.

    Thanks,

    Des

  10. #10
    From the start your message implied it was a folder under the deleted folder (which makes things far more complicated). However if the parent folder is the default Inbox, change the Deleted Folder to the default Inbox Folder e.g. as follows. If you want to permanently delete them remove the apostrophes fro the commented out lines, otherwise they go to the deleted folder. If the folder is several levels down see the thread http://www.vbaexpress.com/forum/show...ead-in-outlook which shows how to concatenate folders paths.

    Option Explicit
    Sub EmptyMyFolder()
        Dim olMyFolder As Outlook.Folder
        Dim olDeletedFolder As Outlook.Folder
        Dim olItems As Outlook.Items
        Dim olItem As Object
        Dim olDelItem As Object
        Dim i As Long, j As Long
        On Error Resume Next
        Set olMyFolder = Session.GetDefaultFolder(olFolderInbox).folders("MyFolder")
        'Set olDeletedFolder = Session.GetDefaultFolder(olFolderDeletedItems)
        Set olItems = olMyFolder.Items
        For i = olItems.Count To 1 Step -1
            Set olItem = olItems(i)
            'olItem.categories = "DEL"
            'olItem.Save
            olItem.Delete
            DoEvents
            'For j = olDeletedFolder.Items.Count To 1 Step -1
            '    Set olDelItem = olDeletedFolder.Items(j)
            '    If olDelItem.categories = "DEL" Then
            '        olDelItem.Delete
            '        Exit For
            '    End If
            'Next j
        Next i
        Set olMyFolder = Nothing
        Set olDeletedFolder = Nothing
        Set olItems = Nothing
        Set olItem = Nothing
    lbl_Exit:
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  11. #11
    Hi Graham, I will test this and let you know. Thanks very much for the help.

  12. #12
    Quote Originally Posted by gmayor View Post
    From the start your message implied it was a folder under the deleted folder (which makes things far more complicated). However if the parent folder is the default Inbox, change the Deleted Folder to the default Inbox Folder e.g. as follows. If you want to permanently delete them remove the apostrophes fro the commented out lines, otherwise they go to the deleted folder. If the folder is several levels down see the thread http://www.vbaexpress.com/forum/show...ead-in-outlook which shows how to concatenate folders paths.

    Option Explicit
    Sub EmptyMyFolder()
        Dim olMyFolder As Outlook.Folder
        Dim olDeletedFolder As Outlook.Folder
        Dim olItems As Outlook.Items
        Dim olItem As Object
        Dim olDelItem As Object
        Dim i As Long, j As Long
        On Error Resume Next
        Set olMyFolder = Session.GetDefaultFolder(olFolderInbox).folders("MyFolder")
        'Set olDeletedFolder = Session.GetDefaultFolder(olFolderDeletedItems)
        Set olItems = olMyFolder.Items
        For i = olItems.Count To 1 Step -1
            Set olItem = olItems(i)
            'olItem.categories = "DEL"
            'olItem.Save
            olItem.Delete
            DoEvents
            'For j = olDeletedFolder.Items.Count To 1 Step -1
            '    Set olDelItem = olDeletedFolder.Items(j)
            '    If olDelItem.categories = "DEL" Then
            '        olDelItem.Delete
            '        Exit For
            '    End If
            'Next j
        Next i
        Set olMyFolder = Nothing
        Set olDeletedFolder = Nothing
        Set olItems = Nothing
        Set olItem = Nothing
    lbl_Exit:
        Exit Sub
    End Sub

    Hi Graham,

    That worked perfectly so thanks very much indeed. I am marking this as solved.

    Cheers,

    Des

Posting Permissions

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