vba - 设置子文件夹时出现问题

标签 vba outlook

我是 Outlook VBA 新手,很难弄清楚如何在下面的代码中设置我的子文件夹。我一整天都在解决这个问题。不确定我错过了什么。

Sub DeleteOlderThan6months()

Dim oFolder As Folder
Dim Date6months As Date
Dim ItemsOverMonths As Outlook.Items

Dim DateToCheck As String

Date6months = DateAdd("d", -1, Now())
Date6months = Format(Date6months, "mm/dd/yyyy")


Set oFolder = oFolder.Folders("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="efa296af8a828e8683c18c8082" rel="noreferrer noopener nofollow">[email protected]</a>").Folders("Inbox").Folders("Zip Files") 

DateToCheck = "[Received] <= """ & Date6months & """"

Set ItemsOverMonths = oFolder.Items.Restrict(DateToCheck)

For i = ItemsOverMonths.Count To 1 Step -1
ItemsOverMonths.Item(i).Delete
Next


Set ItemsOverMonths = Nothing
Set oFolder = Nothing


End Sub

最佳答案

这应该可以,看看我如何设置子文件夹

Option Explicit
Sub DeleteOlderThan6months()
    '//  Declare variables
    Dim oFolder As Folder
    Dim Date6months As Date
    Dim ItemsOverMonths As Outlook.Items
    Dim DateToCheck As String
    Dim olNs As Outlook.NameSpace
    Dim Inbox  As Outlook.MAPIFolder
    Dim oItem As Object
    Dim i As Long

    '// set your inbox and subfolder
    Set olNs = Application.GetNamespace("MAPI")
    Set Inbox = olNs.GetDefaultFolder(olFolderInbox)
    Set oFolder = Inbox.Folders("Zip Files")

    Date6months = DateAdd("d", -1, Now())
    Date6months = Format(Date6months, "mm/dd/yyyy")

    DateToCheck = "[Received] <= """ & Date6months & """"
    Set ItemsOverMonths = oFolder.Items.Restrict(DateToCheck)

    '// Loop through the Items in the folder backwards
    For i = ItemsOverMonths.Count To 1 Step -1
        Set oItem = ItemsOverMonths.Item(i)
        If TypeOf oItem Is Outlook.MailItem Then
            Debug.Print oItem.Subject
            oItem.Delete
        End If
    Next

    Set ItemsOverMonths = Nothing
    Set oFolder = Nothing

End Sub

关于vba - 设置子文件夹时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37060954/

相关文章:

html - 响应式 Outlook 签名

vba - 在 Outlook 邮件中添加多个抄送

javascript - CSS 样式和 Jscript 未在 MS Outlook 2007 中正确反射(reflect)

vba - 如何将 excel UsedRange 转换为从单元格 A1 开始其工作表(即删除 UsedRange 之外的所有空白行和列)?

xml - 使用 VBA 获取 XML 中的所有属性和值

vba - Excel - 使用 VBA 插入公式

vba - Excel VBA 数组循环故障排除 : Using Redim and UBound, 一维和二维数组

php - 使用 php 读取 .pst 文件的内容

c# - 通过 C# 应用程序创建 Outlook 电子邮件草稿

excel - Excel 中的条件连接