检测Outlook附件文件名中的字符串

时间:2014-01-01 01:07:17

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

我正在尝试检测文件名中是否有特定的字符串。

例如,附件文件名为"myimportantplaylist.xlsx"(文件扩展名无关紧要),我的关键字为playlist(加号或减号空格或单词的任意组合)。

我想通过regexp这样做。我有以下构造,我希望将关键字放入。

Public Sub AttachmentQuery(ByVal attachment As Outlook.Attachment, _
  ByVal mailItem As Outlook.MailItem, ByRef Cancel As Boolean)

  Dim frm As New Form1()

  If (System.IO.Path.GetExtension(attachment.FileName) = ".xls") Or _
    (System.IO.Path.GetExtension(attachment.FileName) = ".xls") Or _
    (System.IO.Path.GetExtension(attachment.FileName) = ".doc") Or _
    (System.IO.Path.GetExtension(attachment.FileName) = ".docx") Or _
    (System.IO.Path.GetExtension(attachment.FileName) = ".txt") Or _
    (System.IO.Path.GetExtension(attachment.FileName) = ".pdf") Or _
    (System.IO.Path.GetExtension(attachment.FileName) = ".xfdl") Or _
    (System.IO.Path.GetFileNameWithoutExtension(attachment.FileName) = "*playlist.*")

Then Cancel=True

1 个答案:

答案 0 :(得分:0)

我会使用String.Contains函数(MSDN Documentation

所以你的部分说

System.IO.Path.GetFileNameWithoutExtension(attachment.FileName) = "*playlist.*"

可以成为(在c#中)

System.IO.Path.GetFileNameWithoutExtension(attachment.FileName).Contains("playlist")

我还建议您使扩展程序检查更易于管理,例如查找某种“允许的扩展名”表。