Outlook 2010 自定义 VBA 脚本将传入的邮件消息移动到特定文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5589665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:58:29  来源:igfitidea点击:

Outlook 2010 custom VBA script to move incoming mail message to a specific folder

vbaoutlook-2010

提问by noxee

I'm trying to create a custom rule for Outlook 2010 that inspect the subject of the email and if it makes a regular expression it's moved into a specific folder.

我正在尝试为 Outlook 2010 创建一个自定义规则,用于检查电子邮件的主题,如果它生成正则表达式,则它会被移动到特定文件夹中。

However when I run the script I get the following error when I try and get an Outlook.Folder object for the folder I want to move the message to:

但是,当我运行脚本时,当我尝试获取我要将邮件移动到的文件夹的 Outlook.Folder 对象时,出现以下错误:

Run-time error '91':
Object variable or With block variable not set

运行时错误“91”:
未设置对象变量或块变量

Below is the VBA script that I am using to check the email subject and move the message to the specified folder if it matches.

下面是我用来检查电子邮件主题并将邮件移动到指定文件夹(如果匹配)的 VBA 脚本。

Sub MoveToETS(Item As Outlook.MailItem)
    Dim Subject As String
    Subject = Item.Subject

    Dim FolderToMoveTo As Outlook.Folder
    Set FolderToMoveTo = GetFolder("ETS")

    If (CheckSubject(Subject, "^[Project|Bug] (\d+?) - \[[UPDATE|NEW|RESOLVED]\]")) Then
        Item.Move (FolderToMoveTo)
    End If
End Sub

Function CheckSubject(Subject As String, PatternToCheck As String)
    Dim ObjRegExp As RegExp
    Dim ObjMatch As Match

    Set ObjRegExp = New RegExp
    ObjRegExp.Pattern = PatternToCheck

    If (ObjRegExp.Text(Subject) = True) Then
        CheckSubject = True
    End If

End Function

Function GetFolder(ByVal FolderName As String) As Outlook.Folder

    Dim ObjFolder As Outlook.Folder

    Set ObjFolder = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Folders("ETS")

    GetFolder = ObjFolder

End Function

回答by paulmorriss

Your last but one line needs to be

你的最后一行需要是

Set GetFolder = ObjFolder

回答by Cameron Scott

In your GetFolderfunction, you also hard-coded the folder name.

在您的GetFolder函数中,您还对文件夹名称进行了硬编码。

Line reads:

一行写着:

Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Folders("ETS")

Should read:

应该读:

Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Folders(FolderName)