Need to search email body for regex using VBA

Posted by user6620 on Stack Overflow See other posts from Stack Overflow or by user6620
Published on 2012-10-18T20:31:15Z Indexed on 2012/10/18 23:01 UTC
Read the original article Hit count: 371

Filed under:
|
|

I am trying to write a script that I can run from a rule withing outlook. The goal of this script is to search the email body for a regex pattern, \d{8}-\d{3}\(E\d\) in this case. I want to take that pattern and see if a folder with the name of the match exists and if not create one. I am not super familiar with VBA as I write mostly in python. I have copied and pasted the following bit of code together but at this time I am unable to get the MsgBox to appear when I send an email with 20120812-001(E3) in the body. I have two different versions below.

--version 2--
Sub Filter(Item As Outlook.MailItem)
Dim Matches, Match
Dim RegEx As New RegExp
RegEx.IgnoreCase = True
RegEx.Pattern = "\d{8}-\d{3}(E\d)"
If RegEx.Test(Item.Body) Then
    MsgBox "Pattern Detected"
End If
End Sub

--Version 1--
Sub ProcessMessage(myMail As Outlook.MailItem)
Dim strID As String
Dim objNS As Outlook.NameSpace
Dim objMsg As Outlook.MailItem
Dim objMatch As Match
Dim RetStr As String

Set objRegExp = New RegExp
objRegExp.Pattern = "\d{8}-\d{3}\(E\d\)"
objRegExp.IgnoreCase = True
objRegExp.Global = True
strID = myMail.EntryID
Set objNS = Application.GetNamespace("MAPI")
Set objMsg = objNS.GetItemFromID(strID)
MsgBox objMsg.Body
Set objMatch = objRegExp.Execute(objMsg.Body)
MsgBox objMatch

End Sub

© Stack Overflow or respective owner

Related posts about regex

Related posts about vba