Importing Outlook Contacts data into Word VBA?

B

Bo Hansson

My environment is: Windows 2000 and upwards, Word 2000 and upwards.
Using Word VBA code - is it possible to import Outlook Contacts data into a
register in my Word VBA application?

/BosseH
 
Z

zkid

Yes.

There are several ways, the easiest being through the address book if you
are just accessing the user's contacts and not delegated contacts from a
network.

From VBA help, type in GetAddress.
 
B

Bo Hansson

Very good news, thanks a lot!

However, when testing the opportunities, I cannot extract address details
like Street Address, Postal Code and City.
Have I missed something?

/BosseH
 
Z

zkid

Please go ahead and check out within VBA help the info. I sent you before for
GetAddress. Then, see the following line within that info. [For a list of
the valid address book property names, see the AddAddress method.] and click
on AddAddress for a complete list of available fields you can use with
Graham's code shown below:

strCode = "<PR_GIVEN_NAME> <PR_SURNAME>" & vbCr
strCode = strCode & "<PR_COMPANY_NAME>" & vbCr
strCode = strCode & "<PR_POSTAL_ADDRESS>" & vbCr

'Let the user choose the name in Outlook

strAddress = Application.GetAddress("", strCode, False, 1, , , True, True)

The VBA help function has a multitude of information, not to mention code
you can actually copy and paste in your modules.
 
B

Bo Hansson

Sorry "zkid" for confusing the audience. But I've experienced that helpful
members of the audience never turn back to a thread they obvious see as
completed, and my follow-up questions have not been paid any attention.
However, now I have two problems.

The first one is that following code does not return any information from
the following elements: "<PR_STREET_ADDRESS>", "<PR_POSTAL_CODE>" and
"<PR_LOCALITY>". All other elements are returned without problems.


strFnamn = "<PR_GIVEN_NAME>"
strEnamn = "<PR_SURNAME>"
strTel = "<PR_HOME_TELEPHONE_NUMBER>"
strEpost = "<PR_EMAIL_ADDRESS>"
strMobil = "<PR_CELLULAR_TELEPHONE_NUMBER>"
strGata = "<PR_STREET_ADDRESS>"
strPostNr = "<PR_POSTAL_CODE>"
strOrt = "<PR_LOCALITY>"

strCode = strFnamn & "$1" & strEnamn & "$2" & strTel & "$3" & strMobil &
"$4" & strEpost & "$5" & strGata & "$6" & strPostNr & "$7" & strOrt

strAddress = Application.GetAddress("", strCode, False, 1, , , True, True)

----------------------------------------------------------------------------
--
My second problem is that my attempt to add an adress fails. This is the
code:

Dim tagIDArray(0 To 8) As String
Dim valueArray(0 To 8) As String

tagIDArray(0) = "PR_DISPLAY_NAME"
tagIDArray(1) = "PR_GIVEN_NAME"
tagIDArray(2) = "PR_SURNAME"
tagIDArray(3) = "PR_HOME_TELEPHONE_NUMBER"
tagIDArray(4) = "PR_CELLULAR_TELEPHONE_NUMBER"
tagIDArray(5) = "PR_EMAIL_ADDRESS"
tagIDArray(6) = "PR_STREET_ADDRESS"
tagIDArray(7) = "PR_POSTAL_CODE"
tagIDArray(8) = "PR_LOCALITY"

valueArray(0) = cboTilltal.Text & " " & tbxEfternamn.Text
valueArray(1) = cboTilltal.Text
valueArray(2) = tbxEfternamn.Text
valueArray(3) = tbxFTelefon.Text
valueArray(4) = tbxFMobil.Text
valueArray(5) = tbxEpost.Text
valueArray(6) = tbxGata.Text
valueArray(7) = tbxPostNr.Text
valueArray(8) = tbxPostOrt.Text

Application.AddAddress TagID:=tagIDArray(), Value:=valueArray()

The result is a message saying that "It was not possible to add the address
to the private address book".

Hope again for your assistance.

/BosseH







zkid said:
Please go ahead and check out within VBA help the info. I sent you before for
GetAddress. Then, see the following line within that info. [For a list of
the valid address book property names, see the AddAddress method.] and click
on AddAddress for a complete list of available fields you can use with
Graham's code shown below:

strCode = "<PR_GIVEN_NAME> <PR_SURNAME>" & vbCr
strCode = strCode & "<PR_COMPANY_NAME>" & vbCr
strCode = strCode & "<PR_POSTAL_ADDRESS>" & vbCr

'Let the user choose the name in Outlook

strAddress = Application.GetAddress("", strCode, False, 1, , , True, True)

The VBA help function has a multitude of information, not to mention code
you can actually copy and paste in your modules.

zkid said:
Yes.

There are several ways, the easiest being through the address book if you
are just accessing the user's contacts and not delegated contacts from a
network.

From VBA help, type in GetAddress.
 
J

Jean-Guy Marcil

Bo Hansson was telling us:
Bo Hansson nous racontait que :
Sorry "zkid" for confusing the audience. But I've experienced that
helpful members of the audience never turn back to a thread they
obvious see as completed, and my follow-up questions have not been
paid any attention. However, now I have two problems.

The first one is that following code does not return any information
from the following elements: "<PR_STREET_ADDRESS>",
"<PR_POSTAL_CODE>" and "<PR_LOCALITY>". All other elements are
returned without problems.


strFnamn = "<PR_GIVEN_NAME>"
strEnamn = "<PR_SURNAME>"
strTel = "<PR_HOME_TELEPHONE_NUMBER>"
strEpost = "<PR_EMAIL_ADDRESS>"
strMobil = "<PR_CELLULAR_TELEPHONE_NUMBER>"
strGata = "<PR_STREET_ADDRESS>"
strPostNr = "<PR_POSTAL_CODE>"
strOrt = "<PR_LOCALITY>"

strCode = strFnamn & "$1" & strEnamn & "$2" & strTel & "$3" &
strMobil & "$4" & strEpost & "$5" & strGata & "$6" & strPostNr & "$7"
& strOrt

strAddress = Application.GetAddress("", strCode, False, 1, , , True,
True)

Your code, used as posted, worked on my machine.

Are you sure the info is entered in the appropriate fields in Outlook?
GetAddress extracts the info from the Address fields flagged as the mailing
address in Outlook. So you may enter info in the Home address fields, but if
the Business address is empty while being flagged as the mailing address,
GetAdress return empty handed..

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
B

Bo Hansson

Yes, as far as I can see the info is entered correctly. All is in the Home
address fields and Home is flgged as the mailing address.
I'm running Outlook 2000.

/BosseH
 
Z

zkid

Hi Bo,

I find it very interesting that it is the last three items on your list that
do not come through (correct me if I'm wrong).

Instead of the Application... code below, try using this simplied version to
see if it works better for you for Office 2000:

strAddress = Application.GetAddress(AddressProperties:=strCode,
DisplaySelectDialog:=1)
 
B

Bo Hansson

Sorry "zkid", I get the same error!

/BosseH

zkid said:
Hi Bo,

I find it very interesting that it is the last three items on your list that
do not come through (correct me if I'm wrong).

Instead of the Application... code below, try using this simplied version to
see if it works better for you for Office 2000:

strAddress = Application.GetAddress(AddressProperties:=strCode,
DisplaySelectDialog:=1)
 
Z

zkid

Do you actually get an error message? Or are you just having the same
problem where it doesn't return the info.?

Would you humor us and use just those last three items in GetAddress and see
if the data is returned? For instance:

strCode = strGata & "$6" & strPostNr & "$7" & strOrt
strAddress = Application.GetAddress(AddressProperties:=strCode,
DisplaySelectDialog:=1)
 
B

Bo Hansson

Hi "zkid"

No, I do not get an error message - I can just see that no data is
..returned.

Have now tried to get just the last three items according to your
suggestion, and this returns a completely empty string. Not even the
separators ($6, $7) are included!

Thanks for your interest in this matter!

/BosseH
 
Z

zkid

This is so strange. Your code works fine for me.

It's as if the fields you are trying to retrieve are from a contact that
just doesn't have any data in those fields. Please don't take offense, but
are you absolutely sure there's data there?

You can try placing the insert address icon on your main menu from Word's
customize menu and see what that derives.

From the view menu, select toolbars, customize. Choose all commands. Find
"Insert Address" - it's an open book icon. Drag it to your main menu and
select okay.

Now click on the icon and choose the contact for which you need the data.
See if it provides you with a complete address. If it does, then there's
something missing from your code that isn't obvious to us. If it doesn't
work, then the only other thing I can suggest is that you create a test
contact and insert information in all the necessary fields. Then, try the
code again and see if it works. If it does, then there's something wrong
with your existing contacts.

Go ahead and start a new thread - title it something like GetAddress not
providing all fields. Make sure you provide your code from the get-go. I
think you need some fresh blood on this one!
 
B

Bo Hansson

I've already responded to this, but the reply has obviously disappeared in
the cyber space...

Once again - many thanks for all effort you have put into my problem.

Now I've also tried the "Insert Adress" button, both with the previous and a
newly created contact. Both returns just the name, no adress details!

I thought I had spoiled something in the Outlook application when I (after a
computer crash) tried to import contacts from the earlier computer setup. I
then got some strange messages about incompatibility. I have therefore
reinstalled my Office 2000 package, but my problem still remains.

I will follow your idea about a new thread. And - once more: MANY THANKS!

/BosseH
 
Z

zkid

Ah, you do indeed have corruption, then. If InsertAddress doesn't work for
you, there is no code available that will cirrect your problem.

This is what you need to do.

Instead of trying to export your contacts from a .pst, export them to an
Excel spreadsheet. Take a look at the spreadsheet and make sure that the
addresses are there. If not, these contacts are beyond repair and you will
need to re-enter the addresses.

Then, once again uninstall and re-install your Outlook setup. Now import
the Excel spreadsheet contacts into your contact folder.
 

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