Unable to Delete Folders from "Deleted Item" Folder

  • Thread starter Thread starter abhi4u1947
  • Start date Start date
A

abhi4u1947

Hi, all

I'm trying to delete folders from the "Deleted Items" folder. I'm
using the below snippet. The "Deleted Items" folder also contains some
custom folders, which I'm not able to clear.


I am working with VSTO-SE in vb.net With Outlook 2007.

Plz reply me the solution.

*********************** Error Details ****************************
System.Runtime.InteropServices.COMException was caught
ErrorCode=-2147352567
Message="Cannot delete this folder. Right-click the folder, and then
click Properties to check your permissions for the folder. See the
folder owner or your administrator to change your permissions."
Source="Microsoft Office Outlook"
StackTrace:
at Microsoft.Office.Interop.Outlook.MAPIFolder.Delete()
at
FileMounter.FileMounter.Installhelper.RemoveMyFolder(Application
application, String folderName) in D:\Development\Project\For Office
2007\25-12-2007\FileMounter\Classes\InstallHelper.vb:line 47

*********************Error Details End *******************************



This is my code snippet for deleting the folders from "Deleted Item"
Folder.

''' <summary>
''' This Function Remove MyFolder from our Session.
''' </summary>
''' <param name="application"></param>
''' <param name="folderName"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Function RemoveMyFolder(ByVal application As
Outlook.Application, Optional ByVal folderName As String = "MyFolder")
As Boolean
Dim tempStr As String = ""
Dim tempFolder As Outlook.MAPIFolder = Nothing
Try
For Each Folder As Outlook.MAPIFolder In
application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox).Parent.Folders
If Folder.Name = folderName.Trim Then
Folder.Delete()
tempStr = Folder.Name
'Releasing the Folder After Delete
Folder = Nothing
tempFolder = GetSubFolder(tempStr,
application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderDeletedItems))
If tempFolder IsNot Nothing Then
tempFolder.Delete()
Return True
End If
Next
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
tempFolder = Nothing
End Function
''' <summary>
''' This Function gets the Subfolder from the given
ParentFolder.
''' Used to avoid Exceptions when Folder doesn't exists.
''' </summary>
''' <param name="folderName">The name of the Folder to
retrieve.</param>
''' <param name="parentFolder">The Parent MAPIFolder Object.</
param>
''' <returns>The Subfolder Object or null.</returns>
Private Function GetSubFolder(ByVal folderName As String,
ByVal parentFolder As Outlook.MAPIFolder) As Outlook.MAPIFolder
Dim subFolder As Outlook.MAPIFolder
For index As Integer = parentFolder.Folders.Count To 1
Step -1
subFolder = parentFolder.Folders(index)
If subFolder.Name = folderName Then
Return subFolder
End If
Next
subFolder = Nothing
Return Nothing
End Function
 
Please do no multipost. If you really need to post to more than 1 group do
so in one post.

For Outlook 2007 you should use Folder and not MAPIFolder, BTW.

I'm assuming that if you use the user interface that you actually can delete
the folder that was deleted.

Do you get a valid tempFolder object?

Does it work any better if you frst release Folder then try to delete
tempFolder?

By release I mean calling Marshal.ReleaseComObject(Folder) and then calling
GC.Collect() and then GC.WaitForPendingFinalizers() before calling the
GetSubFolder() method.

If there was more than one folder to be deleted a For Each loop is not the
way to go. A down counting For loop should be used instead.
 
Back
Top