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
N Save Selected Email Message as .msg File Outlook VBA and Custom Forms 9
HarvMan Toggle between calendar and email in Outlook 365 Using Outlook 2
F Email being marked as Spam by Gmail and not being visible in Outlook Using Outlook 3
G Email time stamp Using Outlook 2
G Schedule recurring email and attachments display Outlook VBA and Custom Forms 3
G Save and Rename Outlook Email Attachments Outlook VBA and Custom Forms 0
B Need to Copy an email to a subfolder Outlook VBA and Custom Forms 2
M How to setup outlook after importing old account information - Entering email account info creates with "(1)" after the account! Using Outlook 1
K Multiple Rules on Single Email Using Outlook 2
F VBA to move email from Non Default folder to Sub folders as per details given in excel file Outlook VBA and Custom Forms 8
e_a_g_l_e_p_i Outlook 2010 How to set default email address for website links Using Outlook 3
O Same email address, same person, names in so many ways Using Outlook 4
D Create new email from the received Email Body with attachment Outlook VBA and Custom Forms 10
D Outlook launch email showing new unread email in reading pane Using Outlook 11
B Zoom automatically next email item (VBA) Outlook VBA and Custom Forms 2
K Outlook adds space to rows in HTML email signatures viewed on mobile Using Outlook 1
M HTML email signature in Outlook Using Outlook 1
M Respond to Outlook Email Outlook VBA and Custom Forms 0
U Outlook locking up when replying to Email Using Outlook 7
S Email Generated from another program and then edited sends original email. Using Outlook 2
G Forward email body to other mail list directly from Exchange server Exchange Server Administration 1
S Unable to extract text from an Outlook email message Using Outlook 2
T Original email text not shown when replying or forwarding the email. Using Outlook 6
H Search Email Header for Content Type Outlook VBA and Custom Forms 1
A VBA Script - Print Date between first email in Category X and last email in Category Y Outlook VBA and Custom Forms 3
R Problem moving file “Email folders.pst” to new PC Using Outlook 5
P Sending email from outlook IMAP to GMAIL where embedded images are added as attachment Using Outlook 1
D auto forward base on email address in body email Outlook VBA and Custom Forms 0
P How did hacker cause link to delete and archive containing email? Using Outlook 6
I 'quote marks' around email name Using Outlook 1
N contact list seen in Contact folder but knot in Address book or when 'TO' button is clicked in new email Using Outlook 0
T Junk Email does not get added to the Blocked Sender List Using Outlook 0
P i-Phone 6s Plus receiving 2 notifications from Outlook email Using Outlook 1
C WARNING - DO NOT USE AN OUTLOOK.COM ADDRESS FOR GOOGLE ACCOUNT RECOVERY EMAIL Using Outlook 10
T Why is every new email pinned (to keep it on top)? Windows 10 Using Outlook 6
M Outlook, send to > mail recipient - results in plain text email Using Outlook 1
P Can I create a Rule that sends me an email when I get a Task? Using Outlook 3
M Issue transferring Outlook 2016 email accounts & settings onto new computer Using Outlook 8
R Auto Assign Category colours to Incoming Emails based on whom the email is addressed Outlook VBA and Custom Forms 3
S Outlook email to configure setup for each mail Outlook VBA and Custom Forms 1
S Macro to extract email addresses of recipients in current drafted email and put into clipboard Outlook VBA and Custom Forms 2
A Is there an ID field you can use to pair a reply to the sent email? Outlook VBA and Custom Forms 4
C Auto Run VBA Code on new email Outlook VBA and Custom Forms 1
M How create a Rule to filter sender's email with more that one @ sign Using Outlook 1
A Prepending Email Addrs with "Display Name <email>" Has Stopped Working Using Outlook 0
Bering Forward selected email without the original email appended Outlook VBA and Custom Forms 0
Aussie Rules Run a Script on an Incoming Email OK and then the Email reverts Outlook VBA and Custom Forms 0
W Reply to email Using Outlook 4
F Junk Email does not get added to the Blocked Sender List Using Outlook 4

Similar threads

Top