Microsoft Office Automation with Visual FoxPro
Manipulating the Contacts folder
Outlook has a built-in address book, the folder called Contacts. It s like a super-Rolodex, capable of keeping track of multiple addresses, phone numbers, and e-mail addresses for each person, represented by a ContactItem object.
Surprisingly, Outlook doesn t use collections to manage the various addresses, phone numbers, and e-mail addresses. Separate properties are used for each, and the number of each type is limited (three addresses, 19 phone numbers, three e-mail addresses). Table 4 lists some of the properties of ContactItem see Help for more, but the pattern should be obvious from the table.
The program in Listing 1 reads the Contacts folder and creates a cursor with an entry for each person, containing the name, selected mailing address, and primary telephone number. You ll find it as MakeContactList.PRG in the Developer Download files available at www.hentzenwerke.com.
Table 4. I m in the book. The ContactItem object represents a person in Outlook s address book.
Property | Type | Description |
FullName | Character | The person s full name, unparsed. The name is also available in a variety of configurations, like LastNameAndFirstName, LastFirstAndSuffix, and so forth. See Help for a complete list. |
FirstName | Character | The person s first name. |
LastName | Character | The person s last name. |
HomeAddress | Character | The person s home address, unparsed. As with name, the components are available individually. Outlook is also capable of doing sophisticated parsing of an address it s given to break it into components. |
HomeAddressStreet | Character | The street portion (first line) of the home address. |
HomeAddressCity | Character | The city portion of the home address. |
HomeAddressState | Character | The state portion of the home address. |
HomeAddressCountry | Character | The country of the home address. |
HomeAddressPostalCode | Character | The postal/ZIP code of the home address. |
BusinessAddress | Character | The person s business address, unparsed. The same individual components are available as for home address. |
SelectedMailingAddress | Numeric | Indicates which address is the primary address for this person. Uses these constants: olNone 0 olBusiness 2 olHome 1 olOther 3
|
HomeTelephoneNumber | Character | The person s home telephone number. |
BusinessTelephoneNumber | Character | The person s business telephone number. |
HomeFaxNumber | Character | The person s home fax number. |
BusinessFaxNumber | Character | The person s business fax number. |
PagerNumber | Character | The person s pager number. |
CarTelephoneNumber | Character | The person s car phone number. |
PrimaryTelephoneNumber | Character | The primary phone number to use for this person. |
Email1Address | Character | The first e-mail address for this person. |
Birthday | Datetime | The person s birthday. |
Listing 1. Collecting contact information. This program reads the Contacts from Outlook and puts them into a VFP cursor.
* Read Outlook contact information into a cursor
#DEFINE olContacts 10
#DEFINE olNone 0
#DEFINE olHome 1
#DEFINE olBusiness 2
#DEFINE olOther 3
LOCAL oNameSpace, oContacts, oContact
LOCAL cFirst, cLast, cAddr, cPhone
IF VarType(oOutlook) <> "O"
* Start or connect to Outlook
* Make it public for demonstration purposes.
RELEASE oOutlook
PUBLIC oOutlook
oOutlook = CreateObject("Outlook.Application")
ENDIF
oNameSpace = oOutlook.GetNameSpace("MAPI")
* Get Contacts folder
oContacts = oNameSpace.GetDefaultFolder( olContacts )
* Create a cursor to hold contact information
CREATE CURSOR ContactInfo ;
(cFirstName C(15), cLastName C(20), mAddress M, cPhoneNum C(30))
* Go through contacts
FOR EACH oContact IN oContacts.Items
WITH oContact
cFirst = .FirstName
cLast = .LastName
cPhone = .PrimaryTelephoneNumber
* Choose the right address. If the primary phone number is empty,
* pick up the phone number associated with this address.
DO CASE
CASE .SelectedMailingAddress = olHome
cAddr = .HomeAddress
IF EMPTY(cPhone)
cPhone = .HomeTelephoneNumber
ENDIF
CASE .SelectedMailingAddress = olBusiness
cAddr = .BusinessAddress
IF EMPTY(cPhone)
cPhone = .BusinessTelephoneNumber
ENDIF
CASE .SelectedMailingAddress = olOther
cAddr = .OtherAddress
IF EMPTY(cPhone)
cPhone = .OtherTelephoneNumber
ENDIF
CASE .SelectedMailingAddress = olNone
cAddr = ""
ENDCASE
INSERT INTO ContactInfo VALUES (cFirst, cLast, cAddr, cPhone)
ENDWITH
ENDFOR
* Show the results
BROWSE
RETURN
Chances are that you ll need to go the other direction, too, and create an Outlook contact from FoxPro data. While this book focuses on FoxPro solutions, be aware that Outlook supports the import of files in many formats directly, and has a pretty snappy and flexible interface to its Import Wizard. Consider exporting contact information from VFP into one of the common formats (like CSV) and then importing it into Outlook from there. Contacts are created like other Outlook items, by using the CreateItem method. The example shown in Listing 2 (MakeContact.PRG in the Developer Download files available at www.hentzenwerke.com) adds a record from the TasTrade Supplier table to Outlook s contacts.
Listing 2. Create an Outlook contact. This program sends data from VFP to Outlook to create a new contact.
* Add supplier information
#DEFINE olContactItem 2
#DEFINE olBusiness 2
LOCAL oNameSpace, oContact
IF VarType(oOutlook) <> "O"
* Start or connect to Outlook
* Make it public for demonstration purposes.
RELEASE oOutlook
PUBLIC oOutlook
oOutlook = CreateObject("Outlook.Application")
ENDIF
oNameSpace = oOutlook.GetNameSpace("MAPI")
* Open Supplier
OPEN DATA _SAMPLES + "TasTrade\Data\TasTrade"
USE Supplier
* Pick a random record for demonstration purposes
GO RAND()* RECCOUNT()
* Create a new contact record
oContact = oOutlook.CreateItem( olContactItem )
WITH oContact
.FullName = Contact_Name
.CompanyName = Company_Name
.BusinessAddressStreet = Address
.BusinessAddressCity = City
.BusinessAddressState = Region
.BusinessAddressPostalCode = Postal_Code
.BusinessAddressCountry = Country
.SelectedMailingAddress = olBusiness
.BusinessTelephoneNumber = Phone
.PrimaryTelephoneNumber = Phone
.BusinessFaxNumber = Fax
.Save()
ENDWITH
USE
RETURN
Copyright 2000 by Tamar E. Granor and Della Martin All Rights Reserved