PDA

View Full Version : Getting the TO-address in Outlook using VBA



jherby
09-04-2012, 05:22 AM
Hi

How can I get the email address when Outlook has not recognized the address as an email?

I use the following code:
Set olObj = Application.ActiveInspector.CurrentItem
HisMail = olObj.Recipients.Item(1).Address

But it only works, when Outlook has recognized the address as an email (i.e. when it is underlined in Outlook). Not when it's just normal text - then I get an error (Run-Time error '440').

Can anybody help me?

jherby
09-07-2012, 04:02 AM
This is still very relevant for me. Can anybody help me?

Crocus Crow
09-07-2012, 09:48 AM
Maybe if you posted complete code rather than a snippet, it would be easier to help you.

BrianMH
09-14-2012, 07:58 AM
olObj.recipients(1).AddressEntry.GetExchangeUser.PrimarySmtpAddress

JP2112
09-14-2012, 12:46 PM
olObj.recipients(1).AddressEntry.GetExchangeUser.PrimarySmtpAddress

As long as OP is using Outlook 2007+.

jherby
09-18-2012, 12:59 AM
Maybe if you posted complete code rather than a snippet, it would be easier to help you.

It is more or less all my code:

Sub CheckMail()

Dim olObj As Object
'Note: Must have set reference to:
'Microsoft Excel 14.0 Object Library
'This is accessed in the VBE Editor
'Under Tools -> References...

Set olObj = Application.ActiveInspector.CurrentItem
HisMail = olObj.Recipients.Item(1).Address
Set olObj = Nothing

MsgBox HisMail

'I plan to do more here...

End Sub


BrianMH's solution didn't work for me. I'm running Outlook 2010 (Professional Plus)

jherby
09-24-2012, 04:00 AM
Any suggestions?

BrianMH
09-25-2012, 03:07 AM
edited my code slightly. Try below. This is assuming the mail item is open. If you are talking about just a selected mail you need to use a different method. I googled GetExchangeUser.PrimarySmtpAddress and it is available in office 2010 as far as I can see.

Sub CheckMail()
Dim hismail As String
Dim olObj As MailItem
'Note: Must have set reference to:
'Microsoft Excel 14.0 Object Library
'This is accessed in the VBE Editor
'Under Tools -> References...

Set olObj = Application.ActiveInspector.CurrentItem
hismail = olObj.Recipients.Item(1).AddressEntry.GetExchangeUser.PrimarySmtpAddress
Set olObj = Nothing

MsgBox hismail

'I plan to do more here...

End Sub

jherby
09-26-2012, 01:47 AM
Hi BrianMH

It still doesn't work for me. I have tried to illustrate my problem in this image:

I really can't figure out how to solve this :-/

jherby
09-26-2012, 01:48 AM
Here's the image:
http://i.imgur.com/auu4e.png

BrianMH
09-26-2012, 02:42 AM
You need to resolve the address first. Try this.


Sub CheckMail()

Dim olObj As Object
'Note: Must have set reference to:
'Microsoft Excel 14.0 Object Library
'This is accessed in the VBE Editor
'Under Tools -> References...

Set olObj = Application.ActiveInspector.CurrentItem
olObj.Recipients.Item(1).Resolve
HisMail = olObj.Recipients.Item(1).Address
Set olObj = Nothing

MsgBox HisMail

'I plan to do more here...

End Sub

jherby
09-26-2012, 04:26 AM
It still does not work :-( I get an run-time error '438'

I have also tried ResolveAll
olObj.Recipients.ResolveAll

But that will delete the email address if it's not in my address book...

BrianMH
09-26-2012, 05:34 AM
If you click debug where does it show you getting runtime error 438?

I can't seem to duplicate your error. When I run the code it automatically resolves the address. Perhaps that is because I'm on an exchange server, are you not?

Maybe there is a workaround. Is there code that precedes this to create the email? What is it you are trying to do and what do you need to do with the email address?

jherby
09-26-2012, 05:54 AM
I get the error when running the line "olObj.Recipients.Item(1).Resolve" (see below).

Sub CheckMail()

Dim olObj As Object
'Note: Must have set reference to:
'Microsoft Excel 14.0 Object Library
'This is accessed in the VBE Editor
'Under Tools -> References...

Set olObj = Application.ActiveInspector.CurrentItem
olObj.Recipients.Item(1).Resolve '<- I GET THE ERROR HERE
HisMail = olObj.Recipients.Item(1).Address
Set olObj = Nothing

MsgBox HisMail

'I plan to do more here...

End Sub

BrianMH
09-26-2012, 06:08 AM
Are you manually typing the address in? If so what happens when you click the check names button?

jherby
09-26-2012, 06:27 AM
Yes. I manually type the address.

The reason I make the code is that I have a data base with email-addresses we have contacted on earlier occasions. I want to lookup the current email that I'm about to write to to see if any of my colleagues has contacted that person and/or organization before.

If I click "Check names" it immediately resolve/underline the address i entered.

BrianMH
09-26-2012, 11:48 AM
Well since you are manually typing the name anyway just hit check names and it will resolve for you and then you shouldn't get the error.

jherby
09-27-2012, 12:56 PM
Yes, but that is an extra step for my colleagues who are using this macro in the end. And they are not to technical minded, so keeping it to a minimum would be good.

But most importantly - it really bothers me that I can't solve this! ;-)

BrianMH
09-27-2012, 02:14 PM
Ok. I have been able to reproduce the issue but only if I work fast enough to beat autoresolve (I think you must have this turned off). It is because when you type an email address it doesn't seem to become a recipient right away. Assuming they will only have one email address and this is part of a specific process you could have them enter the address they are sending to in a message box instead of into the to box. As below

Sub CheckMail()

Dim olObj As MailItem
Dim HisMail As String
'Note: Must have set reference to:
'Microsoft Excel 14.0 Object Library
'This is accessed in the VBE Editor
'Under Tools -> References...


Set olObj = Application.ActiveInspector.CurrentItem
olObj.Recipients.Add (InputBox("Enter an email address"))
olObj.Recipients(1).Resolve
HisMail = olObj.Recipients(1).Address



Set olObj = Nothing

MsgBox HisMail

'I plan to do more here...

End Sub

There seems to be no way of activating check names programmatically so your options are either do as above or have people click check names. By the way it was really driving me nuts too.

jherby
10-05-2012, 12:06 PM
I SOLVED IT! :-) Or actually - I posted the same question here (http://www.add-in-express.com/creating-addins-blog/2009/02/20/outlook-recipients-collection/).

The solution is to save the mail first. Just add
olObj.Save before getting the email address. That is:
Sub CheckMail()

Dim olObj As Object
'Note: Must have set reference to:
'Microsoft Excel 14.0 Object Library
'This is accessed in the VBE Editor
'Under Tools -> References...

Set olObj = Application.ActiveInspector.CurrentItem
olObj.Save
HisMail = olObj.Recipients.Item(1).Address
Set olObj = Nothing

MsgBox HisMail

'I plan to do more here...

End Sub

YES :-)