extract email from message body

G

Guest

Can anyone suggest the best way to open each file in an OUtlook subdirectory
and extract an email address from the body of the email? I have mail coming
back as undeliverable, and want to prune those addresses from our lists, but
the address to which the email was sent is not the same as the return
address, which is usually an automated responder from a mail server that
can't find the address in to:

Basically, I'm trying to write code that will say:
go to the undeliverables subdirectory in Outlook;
for each email, check the body of the email for any/all email address(es)
append the email address to a text file;
 
M

Michael Bauer [MVP - Outlook]

You can search the addresses with the Instr function. If there's no specific
position for an address then search for the @ character, then for the
following ' ', and with InstrRev for the leading ' '. Knowing that both
position, you can then extract the string with the Mid function.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
Quick-Cats - Categorize Outlook data:
<http://www.vboffice.net/product.html?id=2006063&cmd=detail&lang=en&pub=6>

Am Wed, 25 Apr 2007 19:00:02 -0700 schrieb Edward:
 
S

Sue Mosher [MVP-Outlook]

Or use regular expressions.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
D

Dmitry Streblechenko

Actually you don't have to parse anything: what Outlook shows as an NDR body
is dynamically constructed from various recipient properties - look at an
NDR with OutlookSpy (click IMessage button and go to the GetRecipientTable
tab).
You can access these properties using Extended MAPI or Redemption (but not
CDO 1.21 since itt does not provide access to the message recipient table
properties)

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
S

Sue Mosher [MVP-Outlook]

No! <g> I think I've tried to explain regular expressions just once and failed miserably. But MSDN has good material including a tutorial, IIRC.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers


Michael Bauer said:
Do you like to explain them here? :)

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
Quick-Cats - Categorize Outlook data:
<http://www.vboffice.net/product.html?id=2006063&cmd=detail&lang=en&pub=6>

Am Thu, 26 Apr 2007 09:28:16 -0400 schrieb Sue Mosher [MVP-Outlook]:
Or use regular expressions.
 
S

Sue Mosher [MVP-Outlook]

That's if it's a true NDR from an Exchange server. If it's not, then the message body text has to be parsed.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
G

Guest

I can't seem to address the body of the email. I have a routine that cycles
through the index of the Inbox, but am unsure how to actually address the
body of the email. Is it an Item, a message, a body, a PR_body, etc. Can't
seem to address it.

Also, I have a search string, " *@* ", which should work if I can find the
right way to run the body through it. However, the like operator doesn't work
in the AdvancedSearch function.
 
G

Guest

This function only returns a numerical value designating the position of the
beginning of the search string. It doesn't return the search string itself.
Also, unable to use 'like' operator in it, so can't get it to do the search.
 
G

Guest

I tried this:


Instr ("urn:schemas:httpmail:textdescription, " %@% ",

vbTextCompare )

But it only returns an index of the found text.

Can you describe a regular expression that would work?
 
G

Guest

Sue Mosher said:
Or use regular expressions.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



Havent seen your book at the stores, but will go to Amazon for it!
 
G

Guest

I'm not trying to search the address line; I'm trying to search the body of
the email for an embedded address. I'm trying to search for a string ""*@*""
within the body of the identified email. I need to know how to tell Outlook
to look at the body of the selected email.

I think the ccode is something like this:

Instr ("urn:schemas:httpmail:textdescription, " %@% ",

vbTextCompare )

Does that look like it would address the body of the selected email for the
search string?
 
G

Guest

I tried this:


Dim objSch As Search
Const strF As String = "urn:schemas:httpmail:textdescription LIKE
'"urn:schemas:httpmail:textdescription LIKE ' *@* '
Const strS As String = "Inbox"
Const strTag As String = "GotAddress"
Set objSch = Application.AdvancedSearch(Scope:=strS, _
Filter:=strF, Tag:=strTag)

Call writer
End Sub

Public Function writer()

Dim objRsts As Results

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\UndelResults.txt", True)

MsgBox "The search " & objSch.Tag & "has completed."
Set objRsts = objSch.Results
'Display number in Results collection
MsgBox "There are " & objRsts.Count & "address items"
'Write each member of Results to file
For Each Item In objRsts
a.writeline (Item) 'took this out: objRsts.WriteLine Item
'this hasn't been set up yet because
'writeline doesn't point to a file
MsgBox "The writer ran"
Next
a.Close

Didn't work. Any suggestions? Maybe I just wrote it wrong. Can you tell if
the search string is correctly formatted: do the spaces need " around them,
or just spaces? Is & needed between search characters? Your help much
appreciated.
 
S

Sue Mosher [MVP-Outlook]

MSDN has good articles on using on regular expressions.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
G

Guest

Any idea why this snippet wouldn't work:
dim address as string
address = myItem."urn:schemas:httpmail:textdescription"
address = InStr("myMsg", " <*@*> ", compare)
AdrOut = address.Text

Does inStr need a different syntax?

This doesn't work either:

Do While n < filecount 'myFolder.Index.Count
Set myItem = myFolder.Items(n)
Set myMsg = (myItem.urn:schemas:httpmail:textdescription)
'Call SearchInboxFolder
'address = myItem."urn:schemas:httpmail:textdescription"
address = InStr("myMsg", " <*@*> ", compare)
AdrOut = address.Text
 
M

Michael Bauer [MVP - Outlook]

Say, MyItem is a variable for an MailItem - then MyItem.Body contains the
plain body of that item, MyItem.HTMLBody contains the html formatted body.

The other syntax ("urn:schemas:httpmail:textdescription") is necessary only
if you want to use the AdvancedSearch method. After calling that method you
get the result in the AdvancedSearchComplete event:

Sub TestAdvancedSearchComplete()
Dim strF As String
Dim strS As String: strS = "Posteingang"

strF = "urn:schemas:httpmail:textdescription LIKE '%VBOffice%'"

Application.AdvancedSearch(strS, strF)
End Sub

Private Sub Application_AdvancedSearchComplete(ByVal SearchObject As
Outlook.Search)
Debug.Print Found: " & SearchObject.Results.Count
End Sub

You can easily explore all of the properties of an object by using the
object browser (f2); switch from <All Libraries> to Outlook.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
Quick-Cats - Categorize Outlook data:
<http://www.vboffice.net/product.html?id=2006063&cmd=detail&lang=en&pub=6>


Am Tue, 1 May 2007 11:50:00 -0700 schrieb Ed:
 
G

Guest

I cannot figure out how to 'read' the body text string. I have the search
text, and am able to address the mapi folder. Can't get InStr to work; it
returns a boolean.
 

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