MailItem.Delete 拒绝删除“已删除邮件”文件夹中的项目
MailItem.Delete refusing to delete item in Deleted Items folder
不确定为什么以下代码没有从 Deleted Items
文件夹中永久删除 MailItem
。
MailItem
肯定会出现在 Deleted Items
文件夹中,但 Delete
似乎什么也不做。放置第二个 Delete
会导致异常,因为它不存在。
我可以通过 Outlook 手动永久删除 MailItem
。
代码:
Dim oOLapp as Outlook.Application
Dim oMapi as Outlook.NameSpace
Dim oFolder as Outlook.Folder
Dim oMailItem as Outlook.MailItem
oOLapp = GetObject([Class]:="Outlook.Application")
oMapi = oOLapp.GetNameSpace("MAPI")
oFolder = oMapi.oFolders(oMapi.DefaultStore.Displayname).Folders("Deleted Items")
oMailItem = oOLapp.Session.OpenSharedItem("C:\sometestmail.msg")
oMailItem = oMailItem.Move(oFolder) ' Now in Deleted items Folder. Verified
' Do other stuff with email
' .
' .
' .
oMailItem.Delete()
oMailItem = nothing
要永久删除 MailItem
,您必须遍历 Deleted
文件夹中的 Items
。
要做到这一点,请查看以下代码:
Dim oApp As New Outlook.Application
Dim oMapi As Outlook.NameSpace = oApp.GetNamespace("MAPI")
Dim oFolders As Outlook.Folders = oMapi.Folders
Dim oFolder As Outlook.MAPIFolder = oFolders(oMapi.DefaultStore.DisplayName).Folders("Deleted Items")
Dim oMailItem As Outlook.MailItem = CType(oApp.Session.OpenSharedItem("C:\sometestmail.msg"), Outlook.MailItem)
oMailItem.UserProperties.Add("DeleteMe", Outlook.OlUserPropertyType.olText)
oMailItem.Move(oFolder)
oMailItem = Nothing
For Each item As Outlook.MailItem In oFolder.Items
Dim oProperty As Outlook.UserProperty = item.UserProperties.Find("DeleteMe")
If oProperty IsNot Nothing Then
item.Delete()
End If
Next
我的代码将与您的不同,因为我已将 Option Strict On。我建议你这样做。这将有助于长期 运行.
请注意,在将 MailItem
移动到 Deleted
文件夹之前,我将 UserProperty
设置为 MailItem
。这将有助于识别您要永久删除的单个 MailItem
。 如果您想要永久删除 Deleted
文件夹中的所有 MailItems
那么这就是您需要的代码:
Dim oApp As New Outlook.Application
Dim oMapi As Outlook.NameSpace = oApp.GetNamespace("MAPI")
Dim oFolders As Outlook.Folders = oMapi.Folders
Dim oFolder As Outlook.MAPIFolder = oFolders(oMapi.DefaultStore.DisplayName).Folders("Deleted Items")
Dim oMailItem As Outlook.MailItem = CType(oApp.Session.OpenSharedItem("C:\sometestmail.msg"), Outlook.MailItem)
oMailItem.Move(oFolder)
oMailItem = Nothing
For i = oFolder.Items.Count To 1 Step -1
CType(oFolder.Items(i), Outlook.MailItem).Delete()
Next
为了使其正常工作,您需要Deleted
文件夹向后循环。查看 MSDN 文档,它指出:
The Delete method deletes a single item in a collection. To delete all items in the Items collection of a folder, you must delete each item starting with the last item in the folder. For example, in the items collection of a folder, AllItems, if there are n number of items in the folder, start deleting the item at AllItems.Item(n), decrementing the index each time until you delete AllItems.Item(1).
不确定为什么以下代码没有从 Deleted Items
文件夹中永久删除 MailItem
。
MailItem
肯定会出现在 Deleted Items
文件夹中,但 Delete
似乎什么也不做。放置第二个 Delete
会导致异常,因为它不存在。
我可以通过 Outlook 手动永久删除 MailItem
。
代码:
Dim oOLapp as Outlook.Application
Dim oMapi as Outlook.NameSpace
Dim oFolder as Outlook.Folder
Dim oMailItem as Outlook.MailItem
oOLapp = GetObject([Class]:="Outlook.Application")
oMapi = oOLapp.GetNameSpace("MAPI")
oFolder = oMapi.oFolders(oMapi.DefaultStore.Displayname).Folders("Deleted Items")
oMailItem = oOLapp.Session.OpenSharedItem("C:\sometestmail.msg")
oMailItem = oMailItem.Move(oFolder) ' Now in Deleted items Folder. Verified
' Do other stuff with email
' .
' .
' .
oMailItem.Delete()
oMailItem = nothing
要永久删除 MailItem
,您必须遍历 Deleted
文件夹中的 Items
。
要做到这一点,请查看以下代码:
Dim oApp As New Outlook.Application
Dim oMapi As Outlook.NameSpace = oApp.GetNamespace("MAPI")
Dim oFolders As Outlook.Folders = oMapi.Folders
Dim oFolder As Outlook.MAPIFolder = oFolders(oMapi.DefaultStore.DisplayName).Folders("Deleted Items")
Dim oMailItem As Outlook.MailItem = CType(oApp.Session.OpenSharedItem("C:\sometestmail.msg"), Outlook.MailItem)
oMailItem.UserProperties.Add("DeleteMe", Outlook.OlUserPropertyType.olText)
oMailItem.Move(oFolder)
oMailItem = Nothing
For Each item As Outlook.MailItem In oFolder.Items
Dim oProperty As Outlook.UserProperty = item.UserProperties.Find("DeleteMe")
If oProperty IsNot Nothing Then
item.Delete()
End If
Next
我的代码将与您的不同,因为我已将 Option Strict On。我建议你这样做。这将有助于长期 运行.
请注意,在将 MailItem
移动到 Deleted
文件夹之前,我将 UserProperty
设置为 MailItem
。这将有助于识别您要永久删除的单个 MailItem
。 如果您想要永久删除 Deleted
文件夹中的所有 MailItems
那么这就是您需要的代码:
Dim oApp As New Outlook.Application
Dim oMapi As Outlook.NameSpace = oApp.GetNamespace("MAPI")
Dim oFolders As Outlook.Folders = oMapi.Folders
Dim oFolder As Outlook.MAPIFolder = oFolders(oMapi.DefaultStore.DisplayName).Folders("Deleted Items")
Dim oMailItem As Outlook.MailItem = CType(oApp.Session.OpenSharedItem("C:\sometestmail.msg"), Outlook.MailItem)
oMailItem.Move(oFolder)
oMailItem = Nothing
For i = oFolder.Items.Count To 1 Step -1
CType(oFolder.Items(i), Outlook.MailItem).Delete()
Next
为了使其正常工作,您需要Deleted
文件夹向后循环。查看 MSDN 文档,它指出:
The Delete method deletes a single item in a collection. To delete all items in the Items collection of a folder, you must delete each item starting with the last item in the folder. For example, in the items collection of a folder, AllItems, if there are n number of items in the folder, start deleting the item at AllItems.Item(n), decrementing the index each time until you delete AllItems.Item(1).