使用 VBA 的 Outlook 2010 脚本化规则

Outlook 2010 scripted rule using VBA

我正在尝试在 Outlook 2010 中创建一个非常简单(因为我是新手并且正在学习)的脚本化规则。

规则是:如果新的邮件项目来自特定的电子邮件地址,运行我的脚本并停止处理规则。 SCRIPT 检查正文中的字符串。如果找到该字符串,它将电子邮件移动到目标文件夹 1,否则将其移动到目标文件夹 2。

遗憾的是,我似乎无法让脚本(下面的代码)执行任何操作(邮件只会转到收件箱,而不是脚本中指定的任一文件夹)。其中很多是从在线示例中拼凑而成的,所以我并不完全理解,但我想我现在会在研究我没有得到的东西时问这个问题。关于如何让它按预期工作的任何想法?

'Use the MailItem class of item
Public Sub NCRFRule(Item As Outlook.MailItem)

        Dim MAPI As NameSpace  'Don't know what this does
        Dim dest1, dest2 As Folder  'declare destination folders
        Dim newMail As MailItem  'set item type

        'Don't know what this does.
        Set MAPI = GetNamespace("MAPI")

        'Set the destination folders
        Set dest1 = MAPI.Folders("Inbox").Folders("NCRFs")
        Set dest2 = MAPI.Folders("Inbox").Folders("other's NCRFs")

        'Rule if-statement.  If text is found, move mail to dest1 folder
        If InStr(1, newMail.Body, "Your Required Action") <> 0 Then
            newMail.Move dest1
            GoTo cutOut:
        End If

        'If the above If-statement doesn't execute, text wasn't found, 
        'move mail to other destination folder.
        newMail.Move dest2

    cutOut:

    End Sub

注意:此代码在 "ThisOutlookSession" 模块中。

MAPI.Folders("Inbox")

没有这样的文件夹。请改用命名空间或存储 class 的 GetDefaultFolder 方法。

此外,您可能会发现 Getting Started with VBA in Outlook 2010 文章很有帮助。

在 Eugene 解释的基础上进行更改

Set dest1 = MAPI.Folders("Inbox").Folders("NCRFs")
Set dest2 = MAPI.Folders("Inbox").Folders("other's NCRFs")

Set dest1 = MAPI.GetDefaultFolder(olFolderInbox).Folders("NCRFs")
Set dest2 = MAPI.GetDefaultFolder(olFolderInbox).Folders("other's NCRFs")

让那部分工作。然后我不得不删除行

Dim newMail As MailItem  'set item type

并将 "newMail" 的所有实例替换为 "Item"。现在可以使用了!