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

Dex G

Hi,

I need to pull a contact from the Global Address List also, were you
able to do it?

Thanks,
D.
 

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