【问题标题】:Delete Items using Input Box values使用输入框值删除项目
【发布时间】:2020-06-09 06:33:15
【问题描述】:

我正在尝试编写一个宏来提示最终用户输入一个数字,然后清除“已删除邮件”文件夹中超过指定天数的任何内容。

如果我将文件夹更改为收件箱或已发送邮件,则以下代码有效。

我已尝试 oItems.Item(i).ReceivedTime 并得到相同的错误消息。

Sub ClearDeletedItems()
 Dim oDeletedItems As Outlook.Folder
 Dim oFolders As Outlook.Folders
 Dim oItems As Outlook.Items
 Dim i As Long
 Set oDeletedItems = 
Application.Session.GetDefaultFolder(olFolderDeletedItems)
 Set oItems = oDeletedItems.Items
 days = CInt(InputBox("How many days of Deleted Items do you want to 
 keep?"))

For i = oItems.Count To 1 Step -1
    If DateDiff("d", oItems.Item(i).SentOn, Now) > days Then
    oItems.Item(i).Delete
 End If
 Next
End Sub

收到 438 错误 - 对象不支持此属性或方法。

【问题讨论】:

  • ReceivedTimeSentOn 是否遇到相同的错误?只是好奇根据您对您尝试 ReceivedTime 没有成功的评论,代码显示您使用 SentOn 属性
  • 您是否在知道 DeletedItems 文件夹仅包含电子邮件的测试环境中收到此错误?多年来,我没有弄乱过 Outlook 和 API,但如果我没记错的话,您可以在已删除的项目文件夹中拥有约会和会议项目。基本上,在您的调试中,您是否验证了当您看到此错误时,您正在处理的是邮件项目而不是其他类型的项目?
  • ReceivedTimeSentOn 都出现同样的错误。
  • DeletedItems 文件夹只包含邮件项目,我有少量用于测试
  • 并且您已经验证,当您收到此错误时,您正在查看的对象实际上是 MailItem,对吗?

标签: vba outlook


【解决方案1】:

下面的怎么样....

Option Explicit
Public Sub Example()
    Dim DeletedFolder As Outlook.MAPIFolder
    Set DeletedFolder = Application.GetNamespace("MAPI" _
                                  ).GetDefaultFolder(olFolderDeletedItems)

    Dim NumDays As String
        NumDays = InputBox(prompt:="Enter the number of days", _
              Title:="Enter the number of days", Default:="Enter number here")
              Debug.Print NumDays 'Immediate Window
        If Trim(NumDays) = vbNullString Then Exit Sub


    If Not IsNumeric(NumDays) Then
        MsgBox "You must enter a numerical value."
        Exit Sub
    End If

    Dim lngDateDiff As Long
        lngDateDiff =  Now -  NumDays
    Dim Filter As String
        Filter = "[SentOn]  < '" & Month(lngDateDiff) & _
                               "/" & Day(lngDateDiff) & _
                               "/" & Year(lngDateDiff) & "'"

    Dim Items As Outlook.Items
    Set Items = DeletedFolder.Items.Restrict(Filter)

    Debug.Print Items.Count  'Immediate Window

'   // Loop through backwards
    Dim i As Long
    For i = Items.Count To 1 Step -1
        DoEvents
        Debug.Print Items(i) 'Immediate Window
'       Items.Remove i ' un-comment to delete items
    Next

    Set DeletedFolder = Nothing
    Set Items = Nothing
End Sub

【讨论】:

  • 不幸的是,使用它时似乎什么都没有发生,无论我输入什么号码,都没有删除任何内容(文件夹中有多年的电子邮件)–Debug.Print Items.Count(i) 总是回来0
  • Debug.Printer Filter 如果我在 InputBox 提示符处输入值 1,则返回 '12/31/1999'
  • 取消注释以下行 - Items.Remove i
猜你喜欢
  • 1970-01-01
  • 2021-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多