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
V Can one change the formatting of email title blocks? Using Outlook 0
P default font when sending email from browser Using Outlook 1
D VBA Macro to Print and Save email to network location Outlook VBA and Custom Forms 1
B IMAP server rejects sent email - cannot deliver messages Using Outlook 2
TedSch Small vba to kill political email Outlook VBA and Custom Forms 3
X Open Hyperlinks in an Outlook Email Message (Help with Diane's solution) Outlook VBA and Custom Forms 3
e_a_g_l_e_p_i Email notifications changed with Outlook 2021 Using Outlook 8
glnz How to retrieve or redo Verizon.net email password without affecting Outlook connection? Using Outlook 1
Z Copy specific email body text Outlook VBA and Custom Forms 0
D ISOmacro to extract active mail senders name and email, CC, Subject line, and filename of attachments and import them into premade excel spread sheet Outlook VBA and Custom Forms 2
M Outlook 365 refuses to send email Using Outlook 1
B Search and Find Email by Folder Name Outlook VBA and Custom Forms 2
K Closing external IMAP email... Outlook 2013 Using Outlook 0
L Capture email addresses and create a comma separated list Outlook VBA and Custom Forms 5
C Email bomb processing Outlook VBA and Custom Forms 1
O What would be the recommended way to change an email address (family member)? Using Outlook 0
A Outlook 2016 Macro to Reply, ReplyAll, or Forward(but with composing new email) Outlook VBA and Custom Forms 0
L Checking Sender Email Address for trusted domain from list on intranet Outlook VBA and Custom Forms 4
J How do you disable address search box when typing @ in body of email? Using Outlook 0
S HTML Code Embedded in String Within Open Outlook Email Preventing Replace(Application.ActiveInspector.CurrentItem.HTMLBody From Working Outlook VBA and Custom Forms 4
Victor.Ayala Automated way to check the option "Show this folder as an email Address Book" Outlook VBA and Custom Forms 2
D Wrong email address in Outlook 2003 "From" tab in new outgoing emails Using Outlook 4
D Forwarding email based on the attachment file type and specific text found on the attachment file name Outlook VBA and Custom Forms 1
F Forward incoming email with 4 embedded images in the body without original sender Outlook VBA and Custom Forms 22
W Macro to Filter Based on Latest Email Outlook VBA and Custom Forms 6
D Create advanced search (email) via VBA with LONG QUERY (>1024 char) Outlook VBA and Custom Forms 2
C Outlook 2007 Removing then adding account restores junk email processing Using Outlook 0
G Place jpg in body of email Outlook VBA and Custom Forms 1
F Wishlist Outlook suddenly began synchronizing deleted items every time I delete a single email. Using Outlook 2
N Save Selected Email Message as .msg File Outlook VBA and Custom Forms 12
HarvMan Toggle between calendar and email in Outlook 365 Using Outlook 12
F Email being marked as Spam by Gmail and not being visible in Outlook Using Outlook 5
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 11
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 3
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

Similar threads

Top