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

时间:2011-04-08 02:06:05

标签: vba outlook-2010

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

然而,当我运行脚本时,当我尝试获取要将消息移动到的文件夹的Outlook.Folder对象时,我收到以下错误:

  

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

下面是我用来检查电子邮件主题的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

2 个答案:

答案 0 :(得分:4)

你的最后一行必须是

Set GetFolder = ObjFolder

答案 1 :(得分:1)

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

行显示:

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

应阅读:

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