使用输入框值删除项目

时间:2019-06-10 16:07:53

标签: vba outlook outlook-vba

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

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

我尝试了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错误-对象不支持此属性或方法。

1 个答案:

答案 0 :(得分:0)

以下内容......

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