Hello.
I was wondering if there are any ActiveX Controls that can be used to link VBA with Outlook Contacts. I would like to create a form that allows someone to select the Contacts in an address book.
![]() |
Patch reliability is unclear, but widespread attacks make patching prudent. Go ahead and patch, but watch out for potential problems. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » ActiveX Controls for Outlook (OfficeXP)
Are you sure you need an ActiveX solution? This code which originally came from Woody years ago shows a method of getting the contacts list displayed. You can probably hack this to do what you require. The GetAddress command is the one that does all the heavy lifting.
Sub InsertAddressFromOutlook() Dim strCode, strAddress As String Dim iDoubleCR As Integer 'Set up the formatting codes in strCode strCode = "" & vbCr strCode = strCode & "" & vbCr strCode = strCode & "" & vbCr strCode = strCode & ", " & vbCr strCode = strCode & "" & vbCr 'Let the user choose the name in Outlook strAddress = Application.GetAddress("", strCode, False, 1, , , True, True) 'Strip away the final "Australia", if any If Right(strAddress, 10) = "Australia" & vbCr Then strAddress = Left(strAddress, Len(strAddress) - 10) End If 'Eliminate blank lines by looking for two carriage returns in a row iDoubleCR = InStr(strAddress, vbCr & vbCr) While iDoubleCR 0 strAddress = Left(strAddress, iDoubleCR - 1) & Mid(strAddress, iDoubleCR + 1) iDoubleCR = InStr(strAddress, vbCr & vbCr) Wend 'Insert the modified address at the current insertion point Selection.TypeText strAddress End Sub
The GetAddress command has a SelectDialog argument which can allow this.
If you explore the GetAddress help page you may find that modifying the key line of the code to
strAddress = Application.GetAddress(“”, strCode, False, 1, 1, , True, True)
allows you to select multiple recipients via a slightly different dialog.
This has been most helpful. I have 2 more questions,
a) if someone selects a distribution list from this dialog box, is there a (programmatic) way to list all the members of that group?
how can you select names from a public or personal Contact folder? This dialog box only allows you to select from the Global Address List. (Perhaps I should post this question in the Outlook folder).
Thanks!
I don’t know about the first question but I suspect you can get at it because you can view the properties easily enough to see the members of the group. You may need to create a link to Outlook first though so this would require further code.
On the address book question, I can select which address book to look in as shown in the screen capture. Do you not get this too?
Andrew
Thanks for that. Someone on the Outlook board directed me to Knowledge Base Article 287563 which I need to set up to be able to view Outlook Address Books. So I can now see the Address books.
I am still interested to find out how to access the members of a distribution list, as I don’t really understand the Outlook model. If you know anything, that would be great.
Marie-Therese
There is a reply by JohnBF in the thread starting at post 323190 in the Outlook forum that may be helpful – look for the reply with VBA code.
There is a reply by JohnBF in the thread starting at post 323190 in the Outlook forum that may be helpful – look for the reply with VBA code.
Marie-Therese, reading through the thread I’m not clear on whether you are looking for street addresses or e-mail addresses. I don’t think Address Books include street addresses. If you want street addresses, you’ll need to work from the Contacts folders. As you process Contacts or AddressList Items, check the Class of each item to determine how to handle it (air code):
If thisItem.Class = olDistributionList Then …
‘ loop thought DistList members by count as in the code Hans referred to
Else If thisItem.Class = ‘ olContact if Contacts, or olAddressEntry if an AddressList Then …
strAddress = ‘thisItem.BusinessAddress if Contact or thisItem.Address if AddressList, or other applicable Property
Marie-Therese, reading through the thread I’m not clear on whether you are looking for street addresses or e-mail addresses. I don’t think Address Books include street addresses. If you want street addresses, you’ll need to work from the Contacts folders. As you process Contacts or AddressList Items, check the Class of each item to determine how to handle it (air code):
If thisItem.Class = olDistributionList Then …
‘ loop thought DistList members by count as in the code Hans referred to
Else If thisItem.Class = ‘ olContact if Contacts, or olAddressEntry if an AddressList Then …
strAddress = ‘thisItem.BusinessAddress if Contact or thisItem.Address if AddressList, or other applicable Property
Andrew
Thanks for that. Someone on the Outlook board directed me to Knowledge Base Article 287563 which I need to set up to be able to view Outlook Address Books. So I can now see the Address books.
I am still interested to find out how to access the members of a distribution list, as I don’t really understand the Outlook model. If you know anything, that would be great.
Marie-Therese
I don’t know about the first question but I suspect you can get at it because you can view the properties easily enough to see the members of the group. You may need to create a link to Outlook first though so this would require further code.
On the address book question, I can select which address book to look in as shown in the screen capture. Do you not get this too?
This has been most helpful. I have 2 more questions,
a) if someone selects a distribution list from this dialog box, is there a (programmatic) way to list all the members of that group?
how can you select names from a public or personal Contact folder? This dialog box only allows you to select from the Global Address List. (Perhaps I should post this question in the Outlook folder).
Thanks!
Andrew, I am having some trouble extracting the addresses from the strAddress string. Do you know what symbol or character delimits each address? I tried the CR and it almost works. But each entry has a “, ” preceding it. Also, is there a way to determine which addresses are CC, and which are TO?
Thanks!
The code calls the GetAddress dialog with dialog of 2, which allows the user to enter multiple addresses
sAddress = Application.GetAddress _
(addressproperties:=cTags, _
useautotext:=False, _
displayselectdialog:=1, selectdialog:=2)
The result looks like this
Bear;Pooh;;;;;;;;;;;/;
, Piglet;;;;;;;;;;;;;
, Eeoor;;;;;;;;;;;
Each address is separated with a CR and a “, “. Unfortunately, it limits the result to 255 characters, which is not very helpful. Also, there seems to be no way to determine if it is returning a CC or a TO address.
Marie-Therese, I don’t have a problem with valid addresses, I get a return like this mix of addresses and unaddressed contracts:
Michael Beatnik
77 Drink Beer Drive,
Nogales, CA 99999
, Mary Jones
,
, Ivy Walls
,
Are you running your code in a country other then the US, where addressing conventions may be more flexible?
(I hope Michael is adequately censored. )
My address formatting may be different to yours. However, I think you have answered my question. The next address always begins with a CR “, “.
There appears to be no way to distinguish between CC and TO addresses though, or is there? and your result is probably limited to 255 characters also, or is it?
Thanks
What is the format of cTags in your example? I tried this for a little test against my PAB:
cTags = "" & vbCr & "" & vbCr & _ "" & Chr(254)
This gave me pickthorn+comma+space between records within the To box, and pickthorn+comma+space between records within the CC box, but in between the two sets, there is no comma. Instead, I get pickthorn+vbCr. So to parse this, you could:
Dim strAll() As String, strTo() As String, strCC() As String strAll = Split(sAddress, Chr(254) & vbCr) strTo = Split(strAll(0), Chr(254) & ", ") If UBound(strAll) > 0 Then strCC = Split(strAll(1), Chr(254) & ", ") End If
I can’t provide any assurances that this is the most efficient approach, but it does seem to work in my little test. Hope this helps.
What is the format of cTags in your example? I tried this for a little test against my PAB:
cTags = "" & vbCr & "" & vbCr & _ "" & Chr(254)
This gave me pickthorn+comma+space between records within the To box, and pickthorn+comma+space between records within the CC box, but in between the two sets, there is no comma. Instead, I get pickthorn+vbCr. So to parse this, you could:
Dim strAll() As String, strTo() As String, strCC() As String strAll = Split(sAddress, Chr(254) & vbCr) strTo = Split(strAll(0), Chr(254) & ", ") If UBound(strAll) > 0 Then strCC = Split(strAll(1), Chr(254) & ", ") End If
I can’t provide any assurances that this is the most efficient approach, but it does seem to work in my little test. Hope this helps.
We dont’ have any duplicate names, because if someone has the same name as another person, then there is a description of the area they work in (eg “Jscher (ABC)” and “Jscher (XYZ)”. This prevents us sending e-mails to the wrong person.
I am struggling a lot with the Outlook Object Model. I have a lot of trouble writing simple code to lookup the address details of a name, or to look up all the names for a distribution list. I hope it starts to make some sense soon. If you have any sample code to lookup details, or members of the address list then this would be appreciated.
One more comment, is that when the I use the “” tag in the GetAddress dialog box, it only ever returns the name and none of the other address details.
> I am struggling a lot with the Outlook Object Model.
Well, the dialog we’re working with is something of a throwback. Once you retrieve unique names from that dialog, you have a broad array of tools to look them up in Outlook. I guess that doesn’t make it any simpler, though.
> One more comment, is that when the I use the “” tag in the GetAddress dialog box, it only ever returns
> the name and none of the other address details.
Sorry if my example misled you. Since you didn’t post your cTags definition, I just made up something that worked for my testing (Name, Phone, Email).
Jscher, thank you so much! What a simple, and yet effective solution. You really are brilliant. This effectively addresses the limitation of only 255 characters from the GetAddress dialog box.
My last tricky question, is how do I return the names of all the people, if a distribution list is chosen from the GetAddress dialog box. I have been searching through the Outlook Object Model, and I can’t understand how to access the DistListItem. Any suggestions?
(Edited by jscher2000 on 08-Jan-04 15:56. Added the italicized clarification.)
There’s a way to search a folder for the list. Check the VBA help for the Find method of Outlook’s Items collection. If the list isn’t in the default Contacts folder, it will take a bit more work to navigate to it (using the Folders(“First Level”).Folders(“Second Level”)…. method of specifying the path).
I have been trying to follow this but it has gone way over my head because I don’t know Word’s Fields and Object Model at all. And I can see the value in the process you are developing, Marie-Therese. So if I can pester you both with some questions …
GetAddress returns a string. If the user selects both DistList and Individual contact Items, are you saying that the process will have to be to parse each name from the string and then run each DistList name back through Get Address? How will you tell the difference between a DistList name string and a Contact string with an empty Address field?
Is there another Word Method similar to GetAddress that is just ‘GetName’, which can then be run against the Contact Items?
(I also don’t understand how you would run this kind of code against non-default Contact Folders.)
Marie-Therese, I would really like to see you post all your related code as an attachment.
Edited by HansV to move very long code to attachment.
Here is the final code that enables you to select names from an address list, and it puts into the ListView control all the names, addresses, etc…When it looks for the members of the Distribution List, it does not limit it to the Global Address Book, but searches every address list until it finds it.
Thank you to everyone who helped with this.
Edited by HansV to move very long code to attachment.
Here is the final code that enables you to select names from an address list, and it puts into the ListView control all the names, addresses, etc…When it looks for the members of the Distribution List, it does not limit it to the Global Address Book, but searches every address list until it finds it.
Thank you to everyone who helped with this.
I have been trying to follow this but it has gone way over my head because I don’t know Word’s Fields and Object Model at all. And I can see the value in the process you are developing, Marie-Therese. So if I can pester you both with some questions …
GetAddress returns a string. If the user selects both DistList and Individual contact Items, are you saying that the process will have to be to parse each name from the string and then run each DistList name back through Get Address? How will you tell the difference between a DistList name string and a Contact string with an empty Address field?
Is there another Word Method similar to GetAddress that is just ‘GetName’, which can then be run against the Contact Items?
(I also don’t understand how you would run this kind of code against non-default Contact Folders.)
Marie-Therese, I would really like to see you post all your related code as an attachment.
(Edited by jscher2000 on 08-Jan-04 15:56. Added the italicized clarification.)
There’s a way to search a folder for the list. Check the VBA help for the Find method of Outlook’s Items collection. If the list isn’t in the default Contacts folder, it will take a bit more work to navigate to it (using the Folders(“First Level”).Folders(“Second Level”)…. method of specifying the path).
Jscher, thank you so much! What a simple, and yet effective solution. You really are brilliant. This effectively addresses the limitation of only 255 characters from the GetAddress dialog box.
My last tricky question, is how do I return the names of all the people, if a distribution list is chosen from the GetAddress dialog box. I have been searching through the Outlook Object Model, and I can’t understand how to access the DistListItem. Any suggestions?
> I am struggling a lot with the Outlook Object Model.
Well, the dialog we’re working with is something of a throwback. Once you retrieve unique names from that dialog, you have a broad array of tools to look them up in Outlook. I guess that doesn’t make it any simpler, though.
> One more comment, is that when the I use the “” tag in the GetAddress dialog box, it only ever returns
> the name and none of the other address details.
Sorry if my example misled you. Since you didn’t post your cTags definition, I just made up something that worked for my testing (Name, Phone, Email).
We dont’ have any duplicate names, because if someone has the same name as another person, then there is a description of the area they work in (eg “Jscher (ABC)” and “Jscher (XYZ)”. This prevents us sending e-mails to the wrong person.
I am struggling a lot with the Outlook Object Model. I have a lot of trouble writing simple code to lookup the address details of a name, or to look up all the names for a distribution list. I hope it starts to make some sense soon. If you have any sample code to lookup details, or members of the address list then this would be appreciated.
One more comment, is that when the I use the “” tag in the GetAddress dialog box, it only ever returns the name and none of the other address details.
My address formatting may be different to yours. However, I think you have answered my question. The next address always begins with a CR “, “.
There appears to be no way to distinguish between CC and TO addresses though, or is there? and your result is probably limited to 255 characters also, or is it?
Thanks
Marie-Therese, I don’t have a problem with valid addresses, I get a return like this mix of addresses and unaddressed contracts:
Michael Beatnik
77 Drink Beer Drive,
Nogales, CA 99999
, Mary Jones
,
, Ivy Walls
,
Are you running your code in a country other then the US, where addressing conventions may be more flexible?
(I hope Michael is adequately censored. )
The code calls the GetAddress dialog with dialog of 2, which allows the user to enter multiple addresses
sAddress = Application.GetAddress _
(addressproperties:=cTags, _
useautotext:=False, _
displayselectdialog:=1, selectdialog:=2)
The result looks like this
Bear;Pooh;;;;;;;;;;;/;
, Piglet;;;;;;;;;;;;;
, Eeoor;;;;;;;;;;;
Each address is separated with a CR and a “, “. Unfortunately, it limits the result to 255 characters, which is not very helpful. Also, there seems to be no way to determine if it is returning a CC or a TO address.
Andrew, I am having some trouble extracting the addresses from the strAddress string. Do you know what symbol or character delimits each address? I tried the CR and it almost works. But each entry has a “, ” preceding it. Also, is there a way to determine which addresses are CC, and which are TO?
Thanks!
The GetAddress command has a SelectDialog argument which can allow this.
If you explore the GetAddress help page you may find that modifying the key line of the code to
strAddress = Application.GetAddress(“”, strCode, False, 1, 1, , True, True)
allows you to select multiple recipients via a slightly different dialog.
Are you sure you need an ActiveX solution? This code which originally came from Woody years ago shows a method of getting the contacts list displayed. You can probably hack this to do what you require. The GetAddress command is the one that does all the heavy lifting.
Sub InsertAddressFromOutlook() Dim strCode, strAddress As String Dim iDoubleCR As Integer 'Set up the formatting codes in strCode strCode = "" & vbCr strCode = strCode & "" & vbCr strCode = strCode & "" & vbCr strCode = strCode & ", " & vbCr strCode = strCode & "" & vbCr 'Let the user choose the name in Outlook strAddress = Application.GetAddress("", strCode, False, 1, , , True, True) 'Strip away the final "Australia", if any If Right(strAddress, 10) = "Australia" & vbCr Then strAddress = Left(strAddress, Len(strAddress) - 10) End If 'Eliminate blank lines by looking for two carriage returns in a row iDoubleCR = InStr(strAddress, vbCr & vbCr) While iDoubleCR 0 strAddress = Left(strAddress, iDoubleCR - 1) & Mid(strAddress, iDoubleCR + 1) iDoubleCR = InStr(strAddress, vbCr & vbCr) Wend 'Insert the modified address at the current insertion point Selection.TypeText strAddress End Sub
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.