Email Parsing VBA Script for Outlook - NEEDED

Status
Not open for further replies.

Jerrod

Member
Outlook version
Outlook 2010 64 bit
Email Account
POP3
I need an Email Parsing Script for incoming Emails from a specific vendor. I have started the script but am having trouble with CASE 2 and CASE 3. Help please..... See specifics below:

Email Properties:
From: XXXYYYZZZ
To: AAABBBCCC
Subject: Inquiry for Property #999

Body:
Scenario 1
Property: 8505 E. San Pablo Dr
JOHN DOE, LOS ANGELES, CA, (5556021919) called to inquire about property 999

Scenario 2
Property: 8505 E. San Pablo Dr
(5556021919) called to inquire about property 999


Script Requirements:
1. Return:
· Property = 8505 E. San Pablo Dr.
· Name = JOHN DOE
· Phone = 555-602-1919



Email Parsing - Script
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


For i= 1 to 3

With Reg1
Select Case i
Case 1
.Pattern = "Property\s*[:]+\s*(\w*)\s*"
.Global = True

Case 2
.Pattern = "Property\s*[:]+\s*(\w*)\s*"
.Global = True

Case 3
.Pattern = "Property\s*[:]+\s*(\w*)\s*"
.Global = True
End Select

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
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Will the name and phone number always be formatted with upper case or ()? You might be able to look for those, since there isn't a field name to use.

If the formats never change, this select case will work:
With Reg1
Select Case i
Case 1
.Pattern = "(Property\s*[:]+\s*(.*)\s*)"
.Global = False

Case 2
.Pattern = "(([A-Z\s]*),)"
.Global = False

Case 3
.Pattern = "(\(([0-9]*)\))"
.Global = True
End Select



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
    Dim strMatch As String
    Dim strAddress, strPhone, strName

Set olMail = Application.ActiveExplorer().Selection(1)

Set Reg1 = New RegExp

For i = 1 To 3

With Reg1
Select Case i
Case 1
.Pattern = "(Property\s*[:]+\s*(.*)\s*\n)"
.Global = False

Case 2
.Pattern = "(\n([A-Z\s]*),)"
.Global = False

Case 3
.Pattern = "(\(([0-9]*)\))"
.Global = True
End Select


End With
If Reg1.test(olMail.Body) Then

Set M1 = Reg1.Execute(olMail.Body)
For Each M In M1
strMatch = M.SubMatches(1)
strMatch = Replace(strMatch, Chr(13), "")
Select Case i
Case 1
strAddress = strMatch
Case 2
strName = strMatch
Case 3
strPhone = strMatch
End Select


Next


End If

Next

Debug.Print "Address: " & strAddress & vbCrLf & "Name: " & strName & vbCrLf & "Phone: " & Format(strPhone, "000-000-0000")

End Sub
 

Jerrod

Member
Outlook version
Outlook 2010 64 bit
Email Account
POP3
Diane,

Thanks for you expeditious reply. To answer your questions: (1) YES - the Names will ALWAYS be in capitals. (2) And, YES - the Telephone Numbers will ALWAYS parenthesis around them with not spaces between the digits.

Will that effect the code in which you have written at all?

Thanks again for your help! :)
 

Jerrod

Member
Outlook version
Outlook 2010 64 bit
Email Account
POP3
Hey Diane,

If the script you wrote is not showing up in the MS Outlook? Is there something I need to do in order to get it there?

Thanks.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Thanks for you expeditious reply. To answer your questions: (1) YES - the Names will ALWAYS be in capitals. (2) And, YES - the Telephone Numbers will ALWAYS parenthesis around them with not spaces between the digits.

Will that effect the code in which you have written at all?
It should work fine under those conditions - it does here, but there is always a chance that my recreation of the messages is not identical to the actual messages.[DOUBLEPOST=1407889360,1407888968][/DOUBLEPOST]
Hey Diane,

If the script you wrote is not showing up in the MS Outlook? Is there something I need to do in order to get it there?

Thanks.

Where are you looking for it? It's not a run a script rule - it runs on a selected message. If you need it to be a run a script, first test it and verify it works, then it need a little tweaking -

name is

Sub GetValueUsingRegEx(olmail as outlook.mailitem)

delete the set olmail line

and of course, you need to do something in place of the debug.print, which prints to the Immediate window (Ctrl+G to view.)
 

Jerrod

Member
Outlook version
Outlook 2010 64 bit
Email Account
POP3
Thanks Diane,

I appreciate the help. Bare with me as I am not a high level programmer. (1) Where is the data returned (file,etc..)? I have not been able to locate it. (2) How can I forward that data along as another email with the data extracted?
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
paste the code into the VBA editor, make sure you Set reference to VB Script library -Microsoft VBScript Regular Expressions 5.5 - then show the immediate window (View menu or Ctrl+G). Now select a message and then run the code. The immediate window will show you the data it collected.

pop this in right after the debug print line
Code:
Dim objMsg As MailItem
Set objMsg = Application.CreateItem(olMailItem)
With objMsg
  .To = "Alias@domain.com"
  .Subject = "This is the subject"
  .BodyFormat = olFormatPlain ' send plain text message
  .body = "Address: " & strAddress & vbCrLf & "Name: " & strName & vbCrLf & "Phone: " & Format
  
  .Display ' use display for testing,  .send to send automatically
End With
Set objMsg = Nothing
 

Jerrod

Member
Outlook version
Outlook 2010 64 bit
Email Account
POP3
Thanks Diane... I am going to dive into it. I really appreciate the help!
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
J Outlook is not parsing html email, unreadable and attachment not decoded Using Outlook 1
W Reply to email Using Outlook 0
F Junk Email does not get added to the Blocked Sender List Using Outlook 0
B disappearing original when attach to email Using Outlook 1
W September 2020 - No Default Email Client message after Office Update Using Outlook 1
glnz O365 - How to combine the Inboxes for four email accounts into a single Inbox Using Outlook 7
glnz O365 - How to send from acct 2 but showing email name from acct 1 as From - alias? Using Outlook 1
S Macro to move “Re:” & “FWD:” email recieved the shared inbox to a subfolder in outlook Outlook VBA and Custom Forms 0
D Move Email with Attachment to Folder Outlook VBA and Custom Forms 3
E Asking user to select multiple options in a list in an email Outlook VBA and Custom Forms 0
K Use VBA to find Sender and Recipient from Microsfot 365 Journaled Email Items Outlook VBA and Custom Forms 3
B Add ComboBox Value to Body of Email Outlook VBA and Custom Forms 1
A Backup Email Accounts On OutLook For Mac 2016 (Microsoft 365 subscription version) Using Outlook 0
F VBA code to dock Styles whenever I write or edit an email Outlook VBA and Custom Forms 0
G Inbox shows old email Using Outlook 3
L Email with correct To address but displaying name of a related person Using Outlook 0
A Two Inboxes, Same Email? Using Outlook 3
B Adding signature to bottom of VBA reply email Outlook VBA and Custom Forms 1
J To delete the draft email Using Outlook 2
B VBScript doesn't run on Recipient Email Outlook VBA and Custom Forms 2
J Autoreply email recieved from specific sender after deleting some text from body. Using Outlook 0
M Outlook 2013 reminder email by using Outlook vba Outlook VBA and Custom Forms 2
D Sending email from Office 365 alias in Outlook Using Outlook 3
S Macro or plug-in to see if specific person was included in this email Outlook VBA and Custom Forms 3
S Body text of Email from invite date/time Outlook VBA and Custom Forms 7
R Outlook 2010 How do you export 2 email Accounts' 2010 Inbox Files to new computer (2019)? Using Outlook 0
R Auto display of new email does not work on non-default account Outlook VBA and Custom Forms 0
J Automatically forward email and apply template Outlook VBA and Custom Forms 0
L Outlook saved email templates Using Outlook 1
B VBA to convert email to task, insert text of email in task notes, and attach copy of original email Outlook VBA and Custom Forms 4
T How can you include Junk Email in Search Results like you can include Deleted Items? Using Outlook 3
B Extracting email addresses from a folder - how to also get the name of the person the address is for? Using Outlook 5
W Replyin to the reply-to email address Outlook VBA and Custom Forms 0
E How to display "Change Folder" in Change Default Email Delivery Location in Exchange Outlook 2016 Using Outlook 1
M Extract all links from Outlook email, send to Excel Using Outlook 2
O Forward a email with modified body Automatically. Outlook VBA and Custom Forms 0
B Outlook 2003 email sending & receiving suddenly stopped working Using Outlook 3
R List folders in a combo box + select folder + move emails from inbox to that folder + reply to that email Outlook VBA and Custom Forms 1
O Email not leaving Outbox when using Excel VBA to sync Outlook account Outlook VBA and Custom Forms 4
T The non-email functions Using Outlook 8
P Outlook 2013 Word Share doc as Email Attachment now brings up Eudora. Using Outlook 1
C How to rename subject line and forward the email Outlook VBA and Custom Forms 2
R Help Revising VBA macro to delete email over different time span Outlook VBA and Custom Forms 0
J Sent Items Folder NOT Showing Correct From Email Address Using Outlook 0
J Outlook Reply > From > Other Email Address... > Address Not Showing in Sent Items... From Email Outlook VBA and Custom Forms 0
T Column to display which email alias a message was sent to Outlook VBA and Custom Forms 6
Marc2019 Need help please! Cannot Setup my outlook email account on my Mac Outlook 2011 Using Outlook.com accounts in Outlook 2
HarvMan Hotmail - Sending email is undeliverable Using Outlook 4
R Error when trying to forward current email item Outlook VBA and Custom Forms 7
H In outlook 365 POP mail Inbox, email takes a long time or never arrives. Using Outlook 1

Similar threads

Top