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