Log in

View Full Version : [SOLVED:] Delete Sub-folder Contents



dodonohoe
07-12-2016, 07:25 AM
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

gmayor
07-12-2016, 09:28 PM
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

dodonohoe
07-13-2016, 01:11 AM
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

SamT
07-13-2016, 07:51 AM
When your code drills down to the subfolder then something like

i=subfoolder items count to 1 step -1 \
delete itmes(i)

dodonohoe
07-14-2016, 02:03 AM
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

gmayor
07-14-2016, 04:45 AM
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

dodonohoe
07-14-2016, 07:00 AM
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

gmayor
07-14-2016, 09:35 PM
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

dodonohoe
07-15-2016, 04:11 AM
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

gmayor
07-15-2016, 10:26 PM
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/showthread.php?56579-marking-copied-sent-mail-as-read-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

dodonohoe
07-17-2016, 03:10 AM
Hi Graham, I will test this and let you know. Thanks very much for the help.

dodonohoe
07-20-2016, 07:20 AM
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/showthread.php?56579-marking-copied-sent-mail-as-read-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