Hi,
I've tried the code from the link Use RegEx to extract text from an Outlook email message in Excel and also added the references as mentioned in the webpage but getting an error message. I am using Excel 365. I've the Outlook mail opened in the background.
Error is in the line Set olMail = Application.ActiveExplorer().Selection(1)
Full code -
I've tried the code from the link Use RegEx to extract text from an Outlook email message in Excel and also added the references as mentioned in the webpage but getting an error message. I am using Excel 365. I've the Outlook mail opened in the background.
Error is in the line Set olMail = Application.ActiveExplorer().Selection(1)
Full code -
Code:
Sub GetValueUsingRegEx()
' Set reference to VB Script library
' Microsoft VBScript Regular Expressions 5.5
Dim olMail As Outlook.MailItem
Dim Reg1 As RegExp
Dim M1 As MatchCollection
Dim M As Match
Set olMail = Application.ActiveExplorer().Selection(1)
' Debug.Print olMail.Body
Set Reg1 = New RegExp
' \s* = invisible spaces
' \d* = match digits
' \w* = match alphanumeric
With Reg1
.Pattern = "Request to change name on bill."
.Global = True
End With
If Reg1.test(olMail.Body) Then
Set M1 = Reg1.Execute(olMail.Body)
For Each M In M1
' M.SubMatches(1) is the (\w*) in the pattern
' use M.SubMatches(2) for the second one if you have two (\w*)
Debug.Print M.SubMatches(1)
Next
End If
End Sub