4

我在 Outlook VBA 编程方面遇到了一个小问题,想知道是否有解决方案,或者这只是另一个“已知问题”。


上下文

我已配置 Outlook 电子邮件帐户以通过 IMAP 访问我的网络电子邮件提供商。在 Outlook 中,我可以正确查看我的网络电子邮件文件夹。我的提供商的垃圾邮件过滤器将垃圾邮件移入垃圾邮件文件夹。

我想自动将放入垃圾邮件文件夹的邮件移动到我本地 pst 文件中的另一个文件夹中。

我让它工作了 99%(通过下面提供的代码供参考)。


问题

我可以看到垃圾邮件文件夹中有邮件(文件夹名称旁边有一个粗体的未读邮件计数),但 ItemAdd 甚至只会在我单击文件夹时触发。那时,我看到了垃圾邮件文件夹的内容,然后看到所有新的垃圾邮件都被移到了我的本地文件夹中。

除了 ItemAdd 之外,是否还有另一个触发源可以用来运行我的代码而无需单击文件夹?当文件夹的未读计数发生变化时是否会触发事件?


技术细节

  • 视窗 8 操作系统
  • 使用 Outlook 2002(是的,我知道...)
  • 我是一位经验丰富的 C/C++ 开发人员,但在 VBA 方面的经验很少,而且在 Outlook 方面没有经验。

VBA代码:

Public WithEvents myItems As Outlook.Items

Public Sub Application_Startup()

    Dim myNameSpace As Outlook.NameSpace
    Const mailboxName As String = "Mail.com"
    Const subfolderName As String = "Spam"


    ' Reference the items in the MAPI spam folder
    ' Because myOlItems is declared "WithEvents" the ItemAdd event will fire below.
    Set myNameSpace = Application.GetNamespace("MAPI")

    On Error GoTo noSpamFolder
    Set myItems = myNameSpace.Folders(mailboxName).Folders(subfolderName).Items
    On Error GoTo 0

    Exit Sub

noSpamFolder:
    MsgBox "Unable to find folder <" & mailboxName & "/" & subfolderName & ">"

End Sub


Private Sub myItems_ItemAdd(ByVal Item As Object)

    Dim suspectFolder As Outlook.MAPIFolder

    ' Check to make sure it is an Outlook mail message, otherwise
    ' subsequent code will probably fail depending on what type
    ' of item it is.
    If TypeName(Item) = "MailItem" Then

       ' Move message to the 'suspect' folder
       On Error GoTo noSuspectFolder
       Set suspectFolder = Outlook.Session.GetDefaultFolder(olFolderInbox).Folders("suspect")
       On Error GoTo 0

       Item.Move suspectFolder

    End If

    Exit Sub

noSuspectFolder:
    MsgBox "Unable to find folder <suspect> as a sub-folder of default inbox folder"
End Sub
4

2 回答 2

3

在发送邮件并使用您的代码执行此任务后,我正在努力解决类似的问题来移动邮件(谢谢!)。有几个问题仍然需要解决。

首先,这些项目被移动了,但在它们被放入垃圾文件夹之后。这似乎是 IMAP 问题(Gmail),可以通过将邮箱帐户的 Internet 电子邮件设置从“将已删除的项目移动到服务器上的以下文件夹”更改为“标记要删除的项目但不移动它们”来解决自动地”。

The second challenge was, like yours, trigger the code to do its work. In the account configuration the save sent emails option is disabled (since this is automatically performed by the Gmail server). I needed to sync the Sent items (MAPI) folder with the Send items (IMAP) folder. I achieved this by configuring the "send/receive" groups for this email account (in the group All account) and selecting the Sent items folder.

Now this folder is synced without the necessity to open the folder for syncing. I hope that this will also resolve your issue.

Peter

于 2013-11-14T23:31:49.613 回答
1

这是有道理的 - Outlook 中的 IMAP 提供程序仅在通过 Outlook 对象模型选择或访问该文件夹时才同步该文件夹。
我认为除了每隔一段时间轮询一次文件夹(并在两次点击之间释放 MAPIFolder 对象)之外,您无能为力

于 2013-02-15T00:40:26.077 回答