繁体   English   中英

引用不在默认收件箱下的文件夹

[英]Reference a folder not under the default inbox

我尝试了无数种方法来从名为“垃圾邮件摘要”的自定义文件夹中删除超过 14 天的项目。 当我将此文件夹嵌套在olDefaultFolder(Inbox)下时,我已成功完成此操作,但是当我将它放在默认收件箱之外时,我无法引用它,因为我收到了未找到的对象。

这是我所拥有的,我似乎无法弄清楚为什么在引用"fldSpamDigest"时找不到对象

    Dim outapp As Outlook.Application
    Set outapp = CreateObject("outlook.application")
    Dim olitem As Object
    Dim fldSpamDigest As Outlook.MAPIFolder

    Set fldSpamDigest = outapp.GetNamespace("MAPI").Folders("Spam Digests")
    For Each olitem In fldSpamDigest.Items
        If DateDiff("d", olitem.CreationTime, Now) > 14 Then olitem.Delete
    Next

    Set fldSpamDigest = Nothing
    Set olitem = Nothing
    Set outapp = Nothing

GetDefaultFolder(olFolderInbox)是一个快捷方式。

您可以长期引用任何文件夹。

Sub reference_walk_the_path()

    Dim outapp As Outlook.Application
    Set outapp = CreateObject("outlook.application")

    Dim olitem As Object
    Dim fldSpamDigest As Outlook.MAPIFolder

    ' from the default inbox to the parent which is your mailbox
    Set fldSpamDigest = outapp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Parent
    ' from the mailbox to a folder at the same level as the Inbox 
    Set fldSpamDigest = fldSpamDigest.folders("Spam Digests")

    ' or
    ' directly from the mailbox to a folder at the same level as the Inbox 
    'Set fldSpamDigest = outapp.GetNamespace("MAPI").folders("your email address").folders("Spam Digests")

    For Each olitem In fldSpamDigest.Items
        If dateDiff("d", olitem.CreationTime, Now) > 14 Then olitem.Delete                
    Next

    Set fldSpamDigest = Nothing
    Set olitem = Nothing
    Set outapp = Nothing

End Sub
Dim outapp As Outlook.Application
Set outapp = CreateObject("outlook.application")

无需在 Outlook VBA 中创建新的 Outlook Application 实例,只需使用 Application 属性


要引用不在默认收件箱下的文件夹 - 例如

Option Explicit
Public Sub Example()
    Dim olNs As Outlook.NameSpace
    Set olNs = Application.Session

    Dim Digest_Fldr As Outlook.MAPIFolder
    Set Digest_Fldr = olNs.GetDefaultFolder(olFolderInbox) _
                          .Parent.Folders("fldSpamDigest")

    Dim Items As Outlook.Items
    Set Items = Digest_Fldr.Items

    Dim i As Long
    For i = Items.Count To 1 Step -1
        DoEvents
        Debug.Print Items(i).Subject
    Next

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM