Help with VBA code to parse text from email body

R

Rafael

*********Start of Email Body***********
A mail message could not be sent because the following host is unknown:
nnnn.net
The message that caused this notification was:
To: <[email protected]>
From: <[email protected]>
Subject: mySubject
*********End of Email Body**************

I have over 300 emails with a message body similar to the one above. I need
help with a piece of VBA code that will read the body of all the emails in
the folder and retrieve the email address right after the To: word and copy
it to the body of a new email message to be displayed.

Here is what I know how to do:
1) reference the folder containing the email messages
2) reference each item within the folder
3) create the email message and set it body = to all the email retrieved
from the item

Here is what I don't know:

1) search through the text above to parse the To: email out.

Thanks,

Rafael
 
R

Rafael

That got me started so here is what I have so far:

Function ParseText(strSource As String, strLabel As String)
Set myolapp = CreateObject("Outlook.Application")
Set olns = myolapp.GetNamespace("MAPI")
Set myInbox = olns.PickFolder
Set myItems = myInbox.Items
Set myItem = myItems("Test")
Dim intLocLabel As Integer
Dim intLocCRLF As Integer
Dim intLenLabel As Integer
Dim strText As String
strSource = myItem.Body
' intLocLabel = "To:"
'intLocLabel = InStr(strSource, strLabel)
intLocLabel = InStr(strSource, "To:")
intLenLabel = Len(strLabel)
If intLocLabel > 0 Then
intLocCRLF = InStr(intLocLabel, strSource, vbCrLf)
If intLocCRLF > 0 Then
intLocLabel = intLocLabel + intLenLabel
strText = Mid(strSource, _
intLocLabel, _
intLocCRLF - intLocLabel)
Else
intLocLabel = Mid(strSource, intLocLabel + intLenLabel)
End If
End If
ParseText = Trim(strText)
End Function

Sub Search()
myValue = ParseText("To:", "To: <")
MsgBox myValue
End Sub

This returns the following value: (e-mail address removed)>
Is there a way to return just the stuff inside the "<" and ">"?

Thanks,

Rafael
I've posted a code sample at
http://www.outlookcode.com/codedetail.aspx?id=89 that shows how to use
standard VB text functions to parse text that has some data in label/data
pairs.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.slipstick.com
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
http://www.slipstick.com/books/jumpstart.htm
 
S

Sue Mosher [MVP]

You can strip the characters by using the Replace function:

strText = Replace(strText, "<","")
 
R

Rafael

That works thanks!

You can strip the characters by using the Replace function:

strText = Replace(strText, "<","")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top