Problem with GetMessage on other mailboxes.

  • Thread starter Thread starter Ward Horsfall
  • Start date Start date
W

Ward Horsfall

Hi,

I appologize for the length of this post - but it is the easiest way to get
the message across.
I am trying to use some VBA code in Outlook XP to get the senders SMTP
address. The code works
fine if I am processing a message in my own inbox. However if I am accessing
another mailbox
like one I have full control over or say a public folder then I come up with
an error. The
one below is me accessing a mailbox that I have full read/write access to..
What am I doing
wrong? Is there another way I should code for such a situation..

Thanks

Ward.

Code :::: --------


Sub get_sender_smtp_address()

Dim oSession As MAPI.Session
Dim objSender As MAPI.AddressEntry
Dim itm As Outlook.mailitem
Dim strMsg As String
On Error Resume Next
Set itm = Application.ActiveExplorer.Selection.Item(1)

' Create a MAPI session.

Set oSession = CreateObject("MAPI.Session")
oSession.Logon "", "", False, False
Err.Clear
Set objMsg = oSession.GetMessage(itm.EntryID)

' The call above fails if not own own mailbox.
'
' Err Code : -2147220991 if not own mailbox.
' [MAPI - [MAPI_E_UNKNOWN_ENTRYID(80040201)]]

' Works fine if sitting in current owned inbox.

If Err <> 0 Then
MsgBox Err
MsgBox Err.Description
End If

Set oSndr = objMsg.Sender

'Get the actual email address
sAddress = oSndr.Address

' Check if it is an Exchange object
If Left(sAddress, 3) = "/o=" Then

' Get the SMTP address
strAddressEntryID = oSndr.ID
sAddress =
oSession.GetAddressEntry(strAddressEntryID).Fields(CdoPR_EMAIL).Value
End If

'Display the information in a MsgBox

MsgBox sAddress

oSession.Logoff

Set oNS = Nothing
Set oItm = Nothing
Set obj = Nothing
Set oSession = Nothing
Set oMsg = Nothing
Set oSndr = Nothing

End Sub
 
In general, you'll get more consistent results if you pass both EntryID and StoreID to the GetMessage function. The StoreID tells CDO what part of the folder hierarchy to look in.
--
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
 
Individual items have no StoreID property. You need to get the StoreID from the parent folder.

This is a good example of why it's not a bad idea to get each property value separately. If you'd done it this way, you'd have figured out the problem in no time:

strEntryID = itm.EntryID
strStoreID = itm.StoreID
Set objMsg = oSession.GetMessage(strEntryID, strStoreID)

The second statement needs to be:

strStoreID = itm.Parent.StoreID

--
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
 
Back
Top