正则表达式检测Outlook附件中的文件名

时间:2014-01-01 03:57:54

标签: regex vba outlook-addin outlook-vba outlook-2010

尝试用这两条路线去。目标是让某人附加一个文件,然后在_ItemSend事件上运行Regexp来检测文件名中的某些字符串,然后是Cancel = True或Cancel = False。我有以下代码,但我遇到的基本问题是将mailItem.Attachments转换为String以实际通过Regexp运行它。我收到很多错误,说它无法转换成字符串...任何想法?

  Public Sub Application_ItemSend(ByVal Item As Object, _
    ByRef Cancel As Boolean) Handles Application.ItemSend
    Dim mailItem As Outlook.MailItem = TryCast(Item, Outlook.MailItem)
    If mailItem IsNot Nothing Then
        Dim attachments = mailItem.Attachments
        For Each attachment As Outlook.Attachment In attachments
            AttachmentQuery(attachment, mailItem, Cancel)
        Next attachment
    End If

    Dim instance As String
    Dim AttachmentMatchResults As MatchCollection
    Dim attachments1 = mailItem.Attachments
    instance = attachments1.ToString
    Dim returnValue As String
    returnValue = instance.ToString
    Dim RegexObj2 As New Regex("^.*(names|playlist|roster).*$", RegexOptions.Multiline)
    AttachmentMatchResults = RegexObj2.Matches(returnValue)
    If RegexObj2.IsMatch(returnValue) Then
        MsgBox("A key file has been found", MsgBoxStyle.OkCancel)
        Cancel = True
    Else
        Cancel = False
    End If

1 个答案:

答案 0 :(得分:0)

如果您正在谈论单个附件对象(而不是附件集合对象)的字符串表示/名称,那么您可以使用(对于第i个附件):

mail_item.Attachments(i).FileName