Outlook Macro to get Exchange Info from Mail

M

_M_a_t_t_

Hi, I'm working on a macro in Outlook 2003 SP3 that, when started, displays
an Email in a custom dialog, broken down into elements. The dialog is used
for Email processing. Upgrading Outlook is not possible, and adding addons is
also not possible (other then what is currently installed).

What I'm looking to achieve is, when an Email is from Exchange (company
email), to display the information found when you right click on the senders
name and choose 'Outlook Properties'. Things displayed here, are, for
example, the address, country & telephone number. The sender is not in the
contacts in Outlook. I want to get the contact information from the Email.

I'm accessing the information from the Email with something like this (I
have seen that MAPI can be used, but I have not found an example that work
like this, if someone can correct me here, I'd be greatful):

Dim olkItem As Outlook.MailItem

For Each olkItem In Application.ActiveExplorer.Selection

'Read the info and enter it into the dialog

'Example info: MsgBox olkItem.SenderEmailAddress

Next

How can I access the Exchange information from the mail item? I hope I have
explained this clearly enough.

Thanks for your time.

Office Version:Office 2003 / Operating System:Windows XP
 
K

Ken Slovak - [MVP - Outlook]

You can get the Exchange Global Address List as a member of the
Outlook.AddressLists collection.

You then would have to find the AddressEntry that corresponds to the
sender's AddressEntry in the GAL. From there you could get some of the
property you refer to, just what's exposed for an AddressEntry.

You pretty much would have to iterate the AddressEntries collection in the
GAL AddressList to find one where the name matched the AddressEntry.Name
property.

What is exposed for an AddressEntry does not include telephone numbers,
country, mailing address or a lot of other things. For those properties you
would need to use other methods such as Extended MAPI or Redemption
(www.dimastr.com/redemption) or CDO 1.21 or even LDAP queries.
 
M

_M_a_t_t_

Thanks for the answer Ken.

I can not install the redemption Reference, however I do have Microsoft CDO
1.21 Reference. I can also use MAPI. Can you provide an example of how to
access the information I'm refering to using either (or both)?

In the example I posted, I can access all the elements of Outlook.MailItem.
Its the elements of Outlook.ContactItemI'm wanting to access from the Email
only, and I can not find a way nor an example of how to do this. Only on how
to access the information from the contact (Contacts saved in Outlook Address
Book locally) from Outlook.ContactItem via address book.
 
K

Ken Slovak - [MVP - Outlook]

For MAPI you'd need to use the IAddressBook interface, but I'm not a MAPI
programmer so you can see if Dmitry sees this, start another thread that
might catch his attention, or post in
microsoft.public.win32.programmer.messaging (which is a group for MAPI
programmers).

I haven't done any CDO coding in probably almost 8 years, I couldn't help
with that. You might find something relevant at www.cdolive.com, either in
the sample snippets or sample projects. Sig had a lot of valuable stuff
there, but with Redemption I just don't use CDO because of the highly
restricted security environment in client side CDO. Server side CDO is not
restricted in the same way.
 
D

Dmitry Streblechenko

So d oyo uwant to access EX specific properties of a contact that was create
in the hContacts folder from one of the GAL entries?
Or EX properties of a message sender/recipient?

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

_M_a_t_t_

The contact does not exist in the local address book. What I want to access
is the contact information you can access when right clicking on From name
and choosing Outlook Properties. With non exchange senders, you Display
name, E-mail address, E-mail type, and Internet format. When its an Exchange
mail, you get (provided entered), shipping address, phone number(s), country
etc. Its this information I'd like to access directly from the Email (as the
contact isn't in the local address book, and I'm reading in, parcing and
displaying information from the email). However, I can not install additional
plug-ins. I need to be able to do this with what Outlook already has. I'm
assuming this can be done with Microsoft CDO 1.21 Reference and/or MAPI, but
I need an example of how to access the information from the email and contact
in 1 loop (or perhaps via a sub function called from the main loop. Again I'm
currently using this formation to read out the information from the Email:

Dim olkItem As Outlook.MailItem
For Each olkItem In Application.ActiveExplorer.Selection
'Read the info and enter it into the dialog
'Example info: MsgBox olkItem.SenderEmailAddress
Next

And this works fine (as long as only 1 mail is selected, more then one
causes problems, since each would be processed, but only the last selected
would actually be displayed).

Matt
 
M

_M_a_t_t_

Any chance of an update on this? I have not been ale to find any information
on the Web or in the Help file....
 
D

Dmitry Streblechenko

Create an instance of the MAPI.Session object,
set Session.MAPIOBJECT property to Namespace.MAPIOBJECT from OOM
Reopen the message in CDO 1.21 using Session.GetMessage and the valeu of teh
MailItem.EntryID
Read the Message.Sender property (returnsd AddressEntry).
Use AddressEntry.Fields[] to access the required propeties - you can get the
props tags from OutlookSpy (e.g. click IMAPISession | QueryIdentity)

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

_M_a_t_t_

Thanks for the update Dmitry, however I do not understand what you are
refering to nor how to do what you have stated (I say that I need an example
previously) ..... Any chance you can post a small example?
 
D

Dmitry Streblechenko

I don't have CDO 1.21 installed here, so the script below uses Redemption


set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
for each Item in Application.ActiveExplorer.Selection
set RDOItem = Session.GetMessagefromID(Item.EntryID)
set Sender = RDOItem.Sender
if not (Sender Is Nothing) Then
MsgBox Sender.City
end if
next

The next script is using CDO 1.21, but I did not have a chance to actually
test it

set Session = CreateObject("MAPI.Session")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
for each Item in Application.ActiveExplorer.Selection
set CDOItem = Session.GetMessage(Item.EntryID)
set Sender = CDOItem.Sender
if not (Sender Is Nothing) Then
MsgBox Sender.City
end if
next

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

_M_a_t_t_

Thanks Dmitry, regretfully the CDO example doesn't work... I get an error on
MAPIOBJECT: "Method or data member not found (Error 461)"....
 
D

Dmitry Streblechenko

Do you get that error on reading the Application.Session.MAPIOBJECT or
setting the Session.MAPIOBJECT property.
Where does your code run?

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

_M_a_t_t_

I get the error on:
Session.MAPIOBJECT = Application.Session.MAPIOBJECT

Regretfully the help isn't 'helpful' here, since I can't find any
information about this, or what would be 'correct'.....
 
D

Dmitry Streblechenko

That line contains two statements. One is reading the property. Another one
is setting it. Can you break that line in two to see which one fails?

set temp = Application.Session.MAPIOBJECT
Session.MAPIOBJECT = temp

Is this Access? If, Session intrinsic variable points to an Access object.
Rename the Session variable in the script to somethign like CDOSession.

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

_M_a_t_t_

After splitting and running, I get the error on:

Session.MAPIOBJECT = temp


This is the exact code as I have it entered in the the editor:

---------------------------------------------
Sub MailTest()
Set Session = CreateObject("MAPI.Session")
Set temp = Application.Session.MAPIOBJECT
Session.MAPIOBJECT = temp
For Each Item In Application.ActiveExplorer.Selection
Set CDOItem = Session.GetMessage(Item.EntryID)
Set Sender = CDOItem.Sender
If Not (Sender Is Nothing) Then
MsgBox Sender.City
End If
Next
End Sub
---------------------------------------------

I'm trying to get this working on Outlook 2003 (11.8313.8221) SP3.

Is this all explained somewhere in the help file? I can't seem to find
anything (of use) for doing this......


Thanks.
 
M

_M_a_t_t_

Again, where does your code run?
I'm trying to get this working on Outlook 2003 (11.8313.8221) SP3.

Another words, Outlook VBA (Alt-F11 in Outlook).
 
D

Dmitry Streblechenko

Session is also an intrinsic variable in VBA. Try the following (it does
work)

PR_LOCALITY = &H3A27001E

Set cdoSession = CreateObject("MAPI.Session")

Set temp = Application.Session.MAPIOBJECT

cdoSession.MAPIOBJECT = temp

For Each Item In Application.ActiveExplorer.Selection

Set CDOItem = cdoSession.GetMessage(Item.EntryID)

Set Sender = CDOItem.Sender

If Not (Sender Is Nothing) Then

Set Field = Sender.Fields.Item(PR_LOCALITY)

If Not (Field Is Nothing) Then

MsgBox Field.Value

End If

End If

Next


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

_M_a_t_t_

Hopfully this reply is posted. Tried several time, and for some reason its
not posting.

When I run the code you posted above I get:
Compile error:

Assignment to constant not permitted

on this line:
Set CdoSession = CreateObject("MAPI.Session")
 

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