【问题标题】:Outlook 2010 scripted rule using VBA使用 VBA 的 Outlook 2010 脚本化规则
【发布时间】:2015-08-20 13:47:50
【问题描述】:

我正在尝试在 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”模块中。

【问题讨论】:

    标签: vba email outlook rule inbox


    【解决方案1】:

    以尤金解释的内容为基础,不断变化

    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”。现在可以了!

    【讨论】:

      【解决方案2】:

      MAPI.Folders("收件箱")

      没有这样的文件夹。请改用 Namespace 或 Store 类的 GetDefaultFolder 方法。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-04
        • 2012-02-12
        相关资源
        最近更新 更多