Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Sender's address using VBA

Status
Not open for further replies.

qjd2004

Technical User
Feb 2, 2004
80
GB
Hiya,

I'm saving emails from outlook to an Access DB using VBA. I'm trying to get the sender's email address from emails.

We're on IMAP here and the enterprise address book does not show up in Outlook. I tried using ADO/ADOX to connect to it like a table and it doesn't seem to want to play ball that way.

Finally I'm trying to use CDO collection to get the sender's email address at the same time as I get the other info from the emails. To test this I've found some code that loops through the folder (for me this is an IMAP SP Folder) and for each email should get the sender's address.
Code:
Sub SendersInFolder()
  
Dim conn As ADODB.Connection
Dim rs1 As ADODB.Recordset
Dim objOL As Outlook.Application
Dim olNS As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim ObjSubFolder As Outlook.MAPIFolder
Dim myMailItem As Outlook.MailItem
Dim olItem As Outlook.MailItem
Dim strReport As String

Dim sOut(3) As String
Dim strSQL As String
Dim F%
Dim x
'Dim y
Set objOL = New Outlook.Application
Set olNS = objOL.GetNamespace("MAPI")
Set objFolder = olNS.Folders.Item(1)
Set ObjSubFolder = objFolder.Folders.Item(1)


    For Each olItem In ObjSubFolder.Items
      ' Get the Sender's name and Email address
      strReport = strReport & olItem.SenderName & " / " & GetSenderID(olItem) & vbCrLf
    Next
   
  MsgBox strReport
   
  'Clean Up
  Set olItem = Nothing
  Set myMailItem = Nothing
  Set ObjSubFolder = Nothing
  Set objFolder = Nothing
  Set olNS = Nothing
  Set objOL = Nothing
End Sub

Function GetSenderID(objMsg As Object) As String

Dim strEntryID As String
Dim strStoreID As String
Dim objSession As MAPI.Session
Dim objCDOItem As MAPI.Message
   
    'Get EntryID and StoreID for message
    strEntryID = objMsg.EntryID
    strStoreID = objMsg.Parent.StoreID
   
    'Start CDO session
    Set objSession = CreateObject("MAPI.Session")
    objSession.Logon    ' , , False, False               ', True
   
    'Pass item to CDO and get sender address
    On Error Resume Next
    Set objCDOItem = objSession.GetMessage(strEntryID, strStoreID)
    SenderFromAddress = objCDOItem.Sender.Address
   
    'Destroy variables
    Set objSession = Nothing
    Set objCDOItem = Nothing
End Function
It runs fine until this line
Set objCDOItem = objSession.GetMessage(strEntryID, strStoreID)
and there it says

Runtime Error: -21417221233 (8004010f)
[MAPI-[MAPI_E_NOT_FOUND(8004010f)]]


Can anyone explain why this is? Is it because we're on IMAP here not Exchange? Can someone please help?
 
i had a similar problem way back years ago , i have ahorrible feeling it cant be done because of attempts to stop virus etc. Ill have a dig aroudn for you

Filmmaker, gentleman and Ambasador for London to the sticks.

 
That's a real pain! But I've a feeling that you're right old chap.
I've not had any further success on it either.

Is it possible to download the contents of an enterprise address book to a local contacts folder so I can access it?
 
im still digging , but i can remember however you can return the name and match that with the address book.

i dont have access to it, but if you go to yahoo groups join the group outlook-vba and do a search in the archive for a post by either Chance, Chance1234 or Chance4321. or 5ylac

thats where i posted the problem and got the original answer from.





Filmmaker, gentleman and Ambasador for London to the sticks.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top