Microsoft KB Archive/269617

= How to automatically add contacts to Outlook with Visual FoxPro =

Article ID: 269617

Article Last Modified on 1/27/2005

-

APPLIES TO


 * Microsoft Visual FoxPro 6.0 Professional Edition, when used with:
 * Microsoft Outlook 2000 Standard Edition
 * Microsoft Visual FoxPro 7.0 Professional Edition
 * Microsoft Visual FoxPro 8.0 Professional Edition
 * Microsoft Visual FoxPro 9.0 Professional Edition

-



This article was previously published under Q269617



SUMMARY
Microsoft Outlook is an Automation Server. As such, you can programatically control it from other applications, sending data back and forth.



MORE INFORMATION
The following example demonstrates how to import the Tasmanian Traders sample's Customer table into Outlook's Contacts folder.

Note The following example was written for use with Microsoft Visual FoxPro version 6.0.
 * 1) DEFINE olFolderContacts 10
 * 2) DEFINE olCurrency 14
 * 3) DEFINE olPercent 12

LOCAL loOutlook, loNameSpace, loFolder, loContact, loNewContact

CLOSE DATA ALL USE HOME(2) + &quot;TasTrade\data\customer&quot;

loOutlook = CREATE(&quot;Outlook.Application&quot;) loNamespace = loOutlook.GetNameSpace(&quot;MAPI&quot;) loFolder = loNameSpace.GetDefaultFolder(olFolderContacts)
 * !* Get an object reference to Outlook
 * !* Get the MAPI namespace (the only available one)
 * !* and find the Contacts folder

loContact = loFolder.folders.Add(&quot;Tasmanian Traders&quot;)
 * !* Add a subfolder below the standard Contacts folder

SCAN loNewContact = loContact.Items.Add && Returns an object of type && ContactItem WITH loNewContact .FullName = contact_name .JobTitle = contact_title .CompanyName = company_name *!* This makes the company name appear above the contact name in      *!* Outlook. It will also sort by company name. .FileAs = .CompanyName + &quot; (&quot; + ;        ALLTRIM(.LastName + &quot;, &quot; + .FirstName + &quot; &quot; + .MiddleName) + &quot;)&quot; *!* Unfortunately, the Contacts table does not include email. *!* Note: the property below is Email One Address. This is not clear *!* under some fonts. .Email1Address = &quot;random@nospam.com&quot; .Email1AddressType = &quot;SMTP&quot;

lcAddress = ALLTRIM(address) + CHR(10) + ; ALLTRIM(city) + ; IIF(EMPTY(region), &quot;&quot;, &quot;, &quot; + ALLTRIM(region)) + &quot; &quot; + ; ALLTRIM(postal_code) + ; IIF(country != &quot;USA&quot;, CHR(10) + ALLTRIM(country), &quot;&quot;) .HomeAddress = lcAddress

.BusinessTelephoneNumber = ALLTRIM(phone) .BusinessFaxNumber = ALLTRIM(fax) *!* Create user-defined properties for extra fields .UserProperties.Add(&quot;Max Order Amount&quot;, olCurrency) .UserProperties.Add(&quot;Min Order Amount&quot;, olCurrency) .UserProperties.Add(&quot;Discount&quot;, olPercent) *!* If you use numeric indexes here (1, 2, and 3) you'll     *!* find that the collection is alphabetical. .UserProperties(&quot;Discount&quot;).Value = ROUND(discount / 100, 2) .UserProperties(&quot;Max Order Amount&quot;).Value = max_order_amt .UserProperties(&quot;Min Order Amount&quot;).Value = min_order_amt *!* ContactItem does not appear in Outlook until it is saved. .Save ENDwith ENDscan

CLOSE DATA ALL After the preceding code is run, do the following to view the results:
 * 1) Open Microsoft Outlook and navigate to the Contacts\Tasmanian Traders folder.
 * 2) Double-click a contact to open it. Under the General tab, note that all the fields for each contact, except for the three user-defined fields, are visible.
 * 3) Select the All Fields tab to view the user-defined fields.

For similar code in Microsoft Access, click the article number below to view the article in the Microsoft Knowledge Base:

290792 How to programmatically export Outlook items to Microsoft Access

