Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Outlook Macro to get Exchange Info from Mail

2 views
Skip to first unread message

Ken Slovak - [MVP - Outlook]

unread,
Apr 13, 2010, 9:56:04 AM4/13/10
to
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.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"_M_a_t_t_" <_M_a_...@discussions.microsoft.com> wrote in message
news:277E54C4-966B-42B0...@microsoft.com...
> 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

_M_a_t_t_

unread,
Apr 14, 2010, 3:27:01 AM4/14/10
to
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.

> .
>

Ken Slovak - [MVP - Outlook]

unread,
Apr 14, 2010, 2:45:38 PM4/14/10
to
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.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"_M_a_t_t_" <Ma...@discussions.microsoft.com> wrote in message
news:7201C3A2-8BA6-4A12...@microsoft.com...

Dmitry Streblechenko

unread,
Apr 15, 2010, 2:20:52 AM4/15/10
to
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_a_t_t_" <Ma...@discussions.microsoft.com> wrote in message
news:7201C3A2-8BA6-4A12...@microsoft.com...

_M_a_t_t_

unread,
Apr 16, 2010, 2:38:01 AM4/16/10
to
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_a_t_t_

unread,
Apr 20, 2010, 6:06:01 AM4/20/10
to
Any chance of an update on this? I have not been ale to find any information
on the Web or in the Help file....

Dmitry Streblechenko

unread,
Apr 20, 2010, 1:15:36 PM4/20/10
to

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

"_M_a_t_t_" <Ma...@discussions.microsoft.com> wrote in message

news:43524C5A-3613-4FF6...@microsoft.com...

Dmitry Streblechenko

unread,
Apr 20, 2010, 1:39:23 PM4/20/10
to
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_a_t_t_" <Ma...@discussions.microsoft.com> wrote in message

news:43524C5A-3613-4FF6...@microsoft.com...

_M_a_t_t_

unread,
Apr 21, 2010, 12:40:01 AM4/21/10
to
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?

Dmitry Streblechenko

unread,
Apr 22, 2010, 1:59:07 AM4/22/10
to
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_a_t_t_" <Ma...@discussions.microsoft.com> wrote in message

news:40D07A3E-9F62-42AF...@microsoft.com...

_M_a_t_t_

unread,
Apr 23, 2010, 3:33:01 AM4/23/10
to
Thanks Dmitry, regretfully the CDO example doesn't work... I get an error on
MAPIOBJECT: "Method or data member not found (Error 461)"....

Dmitry Streblechenko

unread,
Apr 26, 2010, 2:08:14 PM4/26/10
to

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_a_t_t_" <Ma...@discussions.microsoft.com> wrote in message

news:6518FD74-EA5D-41EF...@microsoft.com...

_M_a_t_t_

unread,
Apr 27, 2010, 4:53:01 AM4/27/10
to
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'.....

Dmitry Streblechenko

unread,
Apr 27, 2010, 7:21:12 PM4/27/10
to
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_a_t_t_" <Ma...@discussions.microsoft.com> wrote in message

news:B1228788-3D52-4978...@microsoft.com...

_M_a_t_t_

unread,
Apr 28, 2010, 1:02:01 AM4/28/10
to
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.

Dmitry Streblechenko

unread,
Apr 29, 2010, 1:15:43 PM4/29/10
to
Again, where does your code run? Outlook VBA? Access VBA? A standalone exe
in VB6?

--
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
-
"_M_a_t_t_" <Ma...@discussions.microsoft.com> wrote in message

news:9BE555C8-4508-44B4...@microsoft.com...

_M_a_t_t_

unread,
Apr 30, 2010, 12:31:01 AM4/30/10
to
> 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).

Dmitry Streblechenko

unread,
Apr 30, 2010, 1:39:54 PM4/30/10
to
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_a_t_t_" <Ma...@discussions.microsoft.com> wrote in message

news:482BD9B7-9BCA-4F47...@microsoft.com...

_M_a_t_t_

unread,
May 5, 2010, 3:42:01 AM5/5/10
to
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")

Ken Slovak - [MVP - Outlook]

unread,
May 5, 2010, 8:50:32 AM5/5/10
to
Try setting a reference to CDO.DLL in the references for your VBA project.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm

"_M_a_t_t_" <Ma...@discussions.microsoft.com> wrote in message

news:7F8668BC-7890-4214...@microsoft.com...

_M_a_t_t_

unread,
May 6, 2010, 2:18:01 AM5/6/10
to
Hi Ken,

> Try setting a reference to CDO.DLL in the references for your VBA project.

I have this already:
Microsoft CDO 1.21 Library
C:\Program Files\Common Files\System\MSMAPI\1033\CDO.DLL

I'm starting to think this isn't possible......

_M_a_t_t_

unread,
May 6, 2010, 2:17:01 AM5/6/10
to
Hi Ken,

> Try setting a reference to CDO.DLL in the references for your VBA project.

I have this already:
Microsoft CDO 1.21 Library
C:\Program Files\Common Files\System\MSMAPI\1033\CDO.DLL

I'm starting to think this isn't possible.......

_M_a_t_t_

unread,
May 6, 2010, 2:19:01 AM5/6/10
to
Hi Ken,

> Try setting a reference to CDO.DLL in the references for your VBA project.

_M_a_t_t_

unread,
May 6, 2010, 3:46:01 AM5/6/10
to
Found the/a solution.

Adding Option Explicit to the head of the Module, and then Diming each
variable finally got results. Wish someone had mentioned this....

This now brings me to my next two questions:
1. How to go though each element and not cause an error message to be
displayed when nothing is there (for example if
CdoPR_BUSINESS_ADDRESS_POSTAL_CODE is empty, I get an error that allows me to
End or Debug. I'd like to skip this, and just leave it empty)?
2. How to desable to prompt from Outlook asking if I want to allow the macro
to access:
A program is trying to access e-mail addresses you have stored in Outlook.
Do you want to allow this?

I can only allow 1, 2, 5 & 10 Minutes. But not an always allow. I have
digitally signed the macros, and all work without prompting until CDO is
accessed, then the prompt appears.

Ken Slovak - [MVP - Outlook]

unread,
May 6, 2010, 9:54:28 AM5/6/10
to
For 1 you'd add an On Error Resume Next statement and check for IsEmpty() on
the property and check for an error. If you have either you can clear the
error and just move on.

For 2 that's the reason most of stopped using CDO many years ago, the
security. Client side CDO actually has tighter security than the Outlook
object model (server side CDO has none of the security but should not be
installed where Outlook is installed). There are hacks and kludges such as
ExpressClickYes, but I'd rather not use things like that. As a substitute
for CDO most of us have moved on to using Redemption.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"_M_a_t_t_" <Ma...@discussions.microsoft.com> wrote in message

news:4A495EFD-77D9-4343...@microsoft.com...

0 new messages