Results 1 to 11 of 11
  1. #1
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    314
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Extracting email address from sender field

    Is there some simple way to extract and email address from the From: field in an outlook mail message? The property
    'SenderName' only returns the display name, not the actual email address. I want to find the david@frenchcottages.com in the From: field which has this in it:
    David Hitchins [david@frenchcottages.com]
    I can't believe it isn't possible.
    Again VBA seems to not have answers to basic questions and common practices! Anyone know?

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Extracting email address from sender field

    Well, that makes no sense, does it? Particularly when there is such a rich set of properties for the Recipient object. I suppose you could generate a reply and read it out of there...

    Sub RecipAddr()
    Dim myReply As MailItem, strReplyRecip As String
    Set myReply = ActiveInspector.CurrentItem.Reply
    strReplyRecip = myReply.Recipients(1).Address
    myReply.Delete
    Set myReply = Nothing
    MsgBox strReplyRecip
    End Sub

    If that wasn't bad enough, my other suggestion is to use SendKeys and string manipulation to read it out of the Internet headers! See <A target="_blank" HREF=http://www.wopr.com/cgi-bin/w3t/showthreaded.pl?Cat=&Board=out&Number=20030>here</A>. But I hope you find something more elegant.

  3. #3
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    314
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extracting email address from sender field

    Thanks - a nice idea, but it won't work in my case I don't think. What I am doing is creating a VBA routine in Access that scans my Outlook inbox to see if it can recognise NEW mail from existing enquires who are already in my access Contacts table. It would do this by comparing the email addresses and, if there is an exact match, would process the message by transferring the body contents into a new record in my Calls table and link it to that Contact. It then would move the message into another inbox of "processed messages" where I could check it.
    So as there is not really an Active message I wonder if your trick would work? There has got to be a simple way or does VBA indeed have these glaring holes? A clever language but a very patchy one? David

  4. #4
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    314
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extracting email address from sender field

    Thanks a million! Your code gave me the clue which I tried on a hunch (you'd think you could do this sort of stuff with a bit less of the 'hocus pocus' approach wouldn't you?)
    Here is the code snippet which tells all:
    Set fldMain = GetNamespace("MAPI").GetDefaultFolder(olFolderInbo x)

    Set olMsg = fldMain.Items.Find("[Unread] = " & "True")


    strMessage = olMsg.Body
    strFrom = olMsg.Recipients(1).Address
    MsgBox strFrom
    How daft is that??!!
    But it is elegant in a dumb sort of way.
    Thanks
    David

  5. #5
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    314
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extracting email address from sender field

    FORGET what I said. I the solution I posted does not work!
    It still gives the TO: email address not the FROM: Email address. Oh dear...
    David Hitchins

  6. #6
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    314
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extracting email address from sender field

    OK OK OK it does work if you do this...

    Set fldMain = GetNamespace("MAPI").GetDefaultFolder(olFolderInbo x)

    Set olMsg = fldMain.Items.Find("[Unread] = " & "True")

    strMessage = olMsg.Body
    Set olReply = olMsg.Reply

    strFrom = olReply.Recipients(1).Address
    olReply.Delete
    Set olReply = Nothing

    MsgBox strFrom

    Silly me but it is hot here in Melbourne today!
    David

  7. #7
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Extracting email address from sender field

    Change the middle of your code to this to set strFrom:

    Set olReplyMsg = olMSG.Reply
    strFrom = olReplyMsg.Recipients(1).Address
    Set olReplyMsg = Nothing

    It seems daft, but it does work.

  8. #8
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    314
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extracting email address from sender field

    Seems that we both were thinking of the same thing at the same time... on the opposite sides of the globe!
    Thanks..Now for the next bit, searching the database then processing the message then finding the next unread message
    (FindNext?) etc etc etc.. Wish me luck!

  9. #9
    3 Star Lounger
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    314
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Extracting email address from sender field

    just testing something

  10. #10
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Extracting email address from sender field

    Here's a way to use FindNext, but I redefined your fldmain object:

    Dim olMSG As MailItem, olReplyMsg As MailItem, fldmain As Items
    Dim strMessage As String, strFrom As String
    Set fldmain = GetNamespace("MAPI").GetDefaultFolder(olFolderInbo x).Items
    Set olMSG = fldmain.Find("[Unread] = " & "True") '.Items deleted
    checkNext:
    strMessage = olMSG.Body
    Set olReplyMsg = olMSG.Reply
    strFrom = olReplyMsg.Recipients(1).Address
    Set olReplyMsg = Nothing
    MsgBox strFrom & "; " & strMessage
    Set olMSG = fldmain.FindNext
    If TypeName(olMSG) <> "Nothing" Then GoTo checkNext

    End Sub

  11. #11
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Extracting email address from sender field

    Hi,
    Just as a point of interest, you don't actually need the olReply - you can use
    strFrom = olMsg.Reply.Recipients(1).Address
    Still seems like the long way round though!
    Regards,
    Rory

    Microsoft MVP - Excel

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •