Home | Forums | Contact | Search | Syndication  
 [login] [create account]   Monday, July 22, 2024 
Using FileSystemObject to Create a vCard File  
Description:  In this article Thomas Außem shows how to easily create vCard files for SalesLogix contacts. Thomas not only demonstrates how to create vCard files, but also shows an excellent example of how to create and write to text files using the FileSystemObject.

Category:  SalesLogix VBScript Articles
Author:  Thomas Aussem
Submitted:  11/24/2005
Article has been read 47397 times

Rating: - 5.0 out of 5 by 3 users

Using FileSystemObject to Create a vCard File

In this article Thomas Außem shows how to easily create vCard files for SalesLogix contacts. Thomas not only demonstrates how to create vCard files, but also shows an excellent example of how to create and write to text files using the FileSystemObject.


One way to make yourself familiar with VBScript and COM is the using of the simplest COM object in your scripts - the FileSystemObject. I often use the FileSystemObject to create flat files from SalesLogix, e.g. to export the current group into a HTML file.

The function "CreateContact_VCard" demonstrates the using of the FileSystemObject in a simple way. It generates a simple flat file within the vCard format (Electronic Business Card), containing the contact's details.

The function returns TRUE if the vCard file was created successful and it expects a valid contactid as parameter.


Function CreateContact_VCard(sContactID)

    Dim fso, fVCardFile, sFileName
    Dim rs, sSQL
    Dim sErrMsg
    ' check wether sContactID contains any data, if not leave the function
    If Trim(sContactID) = "" Then
           CreateContact_VCard = False
           Exit Function
    End If
    ' set the filename for the vcard-file
    sFileName = "C:\temp\" & sContactID & ".vcf"
    ' errormessage if the file allready exists
    sErrMsg = "File '" & sFileName & "' already exists." & chr(13)
    sErrMsg = sErrMsg & "Would you like to overwrite the existing file?"
    ' create the FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
    ' check wether this file allreadz exists, if so, show a confirmation dialog
    If fso.FileExists(sFileName) Then
           If MsgBox(sErrMsg, vbYesNo,"") = vbNo Then
                  Set fso = Nothing
                  CreateContact_VCard = False
                  Exit Function
           End If
    End If
    ' generate the SQL statement to get all neccessary information for the vCard file
    sSQL = "SELECT account,title,firstname,lastname,homephone,workphone,mobile,"
    sSQL = sSQL & " fax,email,addressid,address1,postalcode,city,country "
    sSQL = sSQL & " FROM contact,address WHERE CONTACTID = '" & sContactID & "'"
    sSQL = sSQL & " AND contact.addressid = address.addressid"

    ' create a new ADO connection and invoke the SQL statement
    Set rs = CreateObject("ADODB.Recordset")
    Set rs.ActiveConnection = Application.GetNewConnection
    rs.open sSQL
    ' open the file for overwriting
    Set fVCardFile = fso.CreateTextFile(sFileName, True)
    ' write all information from SQL statement to the vCard file
    fVCardFile.WriteLine("N:" & rs.Fields("LASTNAME").Value & ";" & _
    fVCardFile.WriteLine("FN:" & rs.Fields("FIRSTNAME").Value & " " & _
    fVCardFile.WriteLine("ORG:" & rs.Fields("ACCOUNT").Value)
    fVCardFile.WriteLine("TITLE:" & rs.Fields("TITLE").Value)
    fVCardFile.WriteLine("TEL;WORK;VOICE:" & rs.Fields("WORKPHONE").Value)
    fVCardFile.WriteLine("TEL;WORK;FAX:" & rs.Fields("FAX").Value)
    fVCardFile.WriteLine("TEL;HOME;VOICE:" & rs.Fields("HOMEPHONE").Value)
    fVCardFile.WriteLine("TEL;CELL:" & rs.Fields("MOBILE").Value)
    fVCardFile.WriteLine("EMAIL;WORK:" & rs.Fields("EMAIL").Value)
    fVCardFile.WriteLine("ADR;HOME:;;" & rs.Fields("ADDRESS1").Value & ";" & rs.Fields("CITY").Value & _
                         ";;" & rs.Fields("POSTALCODE").Value & ";" & rs.Fields("COUNTRY").Value)

    ' close the file and set the FileSystemObject to nothing
    Set fso = Nothing
    Set rs = Nothing
End Function

Additional Information

This script was written in SalesLogix 6.2.

You can find detailed information about the vCard format on http://www.imc.org/pdi/ Detailed information about the FileSystemObject is available in the Microsoft VBScript documentation.

You may combine this script with the Stephen Redmond's article Using the SalesLogix Mail Client to Send E-mail, to attach a vCard file with contact detail information of the logged in SalesLogix user.


About the Author

  Thomas Aussem


Working with SalesLogix 6.2 since December 2004 You'll find my German blog (that deals with SalesLogix, SQL Server and web development) on http://www.opencrms.de

View online profile for Thomas Aussem

[ back to top] [ send to a friend]  

Rate This Article you must log-in to rate articles. [login here] 
Please log in to rate article.

Comments & Discussion you must log-in to add comments. [login here] 
Author Article Comments and Discussion
Paul Pacun

Re: Using FileSystemObject to Create a vCard File
Posted: 4/26/2006 10:27:44 PM

We are going to write an app which will gather all of the vcards in a folder and import them all at once. The fun part will be parameterizing it so we can mark the contacts accordinly + deduping. Harvesting names is fun!!


       Visit the slxdeveloper.com Community Forums!
Not finding the information you need here? Try the forums! Get help from others in the community, share your expertise, get what you need from the slxdeveloper.com community. Go to the forums...

 slxdeveloper.com is brought to you courtesy of Ryan Farley & Customer FX Corporation.
 This site, and all contents herein, are Copyright © 2024 Customer FX Corporation. The information and opinions expressed here are not endorsed by Sage Software.

code of conduct | Subscribe to the slxdeveloper.com Latest Article RSS feed
page cache (param): 7/22/2024 4:00:46 PM