Pull Recipient from Outlook Address book

B

BigPig

Hi All,

I'm trying to get an Outlook display name from an outlook address book and
put it in a text box. I can open the address book, but can't seem to find a
way to get the display name. Any and all help would be greatly appreciated.
See the script below. Thanks -BigPig-

Set appOutlook = CreateObject("Outlook.Application")
Set CDOSession = appOutlook.CreateObject("MAPI.Session")

CDOSession.Logon "", "", False, False, 0

On Error GoTo ErrHand
Set Recipients = CDOSession.addressbook(Nothing, "Address Book", False,
True, 1, "To:", "", "", 0)

Dim OLDN As String
OLDN = Recipients
Stop
txt_SUYI_OutL_Display_Name = OLDN
Set appOutlook = Nothing
Set CDOSession = Nothing

ErrHand:
Set appOutlook = Nothing
Set CDOSession = Nothing
Exit Sub

txt_SUYI_OutL_Display_Name = OLDN

Set appOutlook = Nothing
Set CDOSession = Nothing
 
S

Steve Yandl

Are you trying to get a specific name or the list of names from the address
book?

Here is a sub that would place the names of all your contacts in column A of
the active sheet beginning with A1.

'--------------------------------------------

Sub MyContactNames()

Const olContactFolder = 10

Dim R As Integer

Set objOL = CreateObject("Outlook.Application")
Set olNS = objOL.GetNamespace("MAPI")
Set myFolder = olNS.GetDefaultFolder(olContactFolder)
Set myItems = myFolder.Items

R = 1

For Each myContact In myItems
If TypeName(myContact) = "ContactItem" Then
Cells(R, 1).Value = myContact.FullName
R = R + 1
End If
Next myContact

Set olNS = Nothing
Set objOL = Nothing

End Sub

'--------------------------------------------

Steve Yandl
 
P

Peter T

Did you test that, what is "olContactFolder" ?

try
Set myFolder = olNS.GetDefaultFolder(10) ' olFolderContacts

(assuming the required contacts are in the default folder)

Regards,
Peter T
 
B

BigPig

Steve and Peter,

Thank you for your responses, I can see why it might be necessary to run a
loop. However, I'm on a network and the address book is very large. I just
need the selected outlook display name to go into a textbox. So to reiterate,
I need a specific name, for which the user will type in and select.
 
P

Peter T

Apart from not reading Steves code carefully I didn't read your OP fully. I
have now though and your later clarification, but it's not clear at all. If
the user is going to type in a specific name, what do you want back from
Outlook. What do you mean by "display name"

Maybe you can work things out for your self -

Set a reference to outlook, Tools References, and fully declare your Outlook
objects

Dim myFolder As Outlook.MAPIFolder
Dim myitems As Outlook.Items
Dim myContact As Outlook.ContactItem


Run the original code and return some valid names. Now include the following

dim s as string

Set myItems = myFolder.Items
s = "some valid name"
Set myContact = myItems(s)
Stop
' press Alt-v,s and look at myContact in Locals

Regards,
Peter T
..
 
P

Peter T

Apologies Steve, I didn't notice you had declared the constant

Regards,
Peter T
 
S

Steve Yandl

Are you saying that Outlook is already open to the 'Contacts' folder with a
name selected and you want to discover what that name is?

If not, how will the code be able to determine which of the many entries in
your address book should be selected? Now, if you're saying you want the
user to be able to type in an email address and have the code return the
name of the user who has that email addy or something like that, we can
modify the code to do so.


Steve Yandl
 
B

BigPig

Hi All,

Sorry for being as clear as mud, must be the moniker.

What I'm trying to do, is thru an excel userform, open up the outlook
address book, and thru the outlook address book, search for a name and when
found (should then be in the textbox to the right of "To") click "Ok" on the
outlook address book, then have excel pull that name and place it in an excel
userform's textbox.

The Outlook Display Name is the name that recipients see in emails. I.e.
BigPig versus (e-mail address removed). It really doesn't matter if it's
the smtp email address or the display name, it was only a preference of mine.

To answer the last part of Steve's question: "Now, if you're saying you want
the
user to be able to type in an email address and have the code return the
name of the user who has that email addy or something like that, we can
modify the code to do so. "

Yes, similar to that but, type in the name and have the code return the
display name or email address.

I hope that this is less muddy.

Again, thank you all very much for your patience and sage advice.
 
S

Steve Yandl

When you describe the scenario and state "(should then be in the textbox to
the right of "To")", I get the impression you're talking about the address
line in a new email message. In this case, you're offered a list of names
from the address book (typically the contents of the Contacts folder) but
you're not actually in the address book. There may be a reason why you want
to access address entries this way but there may be an easier way.

Here is something you can try and then let us know if it might be modified
to suit your needs. I created a userform and placed a listbox in it (left
the name ListBox1). I set its properties so it has 2 columns. I made it
400pt wide. For column widths, I used
250pt;150pt

Finally, I set up the following sub for the UserForm Activate event. When
the UserForm is activated, the listbox is populated with a sorted list of my
contacts showing the display names first and then the email address.

'----------------------------------------------

Private Sub UserForm_Activate()

Dim x As Integer

Set objOL = CreateObject("Outlook.Application")
Set olNS = objOL.GetNamespace("MAPI")
Set myFolder = olNS.GetDefaultFolder(10)
Set myItems = myFolder.Items

myItems.Sort "FullName"

x = 0
For Each myContact In myItems
If TypeName(myContact) = "ContactItem" Then
If Len(myContact.Email1DisplayName) > 0 Then
ListBox1.AddItem
ListBox1.Column(0, x) = myContact.Email1DisplayName
ListBox1.Column(1, x) = myContact.Email1Address
x = x + 1
End If
End If
Next myContact


Set olNS = Nothing
Set objOL = Nothing

End Sub


'----------------------------------------------

Steve Yandl
 
C

Charlotte E

Neat little routine :)


Is it possible to extract other information besides the .Fullname?

I've tried with .Email, but it gives an error


TIA,
 
S

Steve Yandl

Charlotte,

Try .email1address

Open Outlook, press Alt plus F11 and then press F2 to get the object
browser. In the list in the left pane, select contactItem and you will
locate a very long list of properties you might look up.


Steve Yandl
 

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