VB.NET Reads a Vcard:
Author: Terry Voss
credit to: Vcard Spec

A Vcard is a specification from PIM=Personal Information Mgmt used by Outlook and many others to transfer business card type info. You create them and consume them. I am concerned with the client consumption here as I could not find such on Internet. See an example of the text of a Vcard below that is the contents of a text file that might be called Terry_Voss.vcf where the extension is for vcard filename.

BEGIN:VCARD
VERSION:2.1
N:Voss;Terry;A;;;
FN:Terry Voss
ADR;HOME:;;2403 N Nettleton St;Spokane;WA;99205;;
EMAIL;INTERNET;PREF:tvoss@computer-consulting.com
TEL;HOME:5093277202
ORG:Computer Consulting;
URL;WORK:http://www.computer-consulting.com
ADR;WORK:;;2403 N Nettleton St;Spokane;WA;99205;;
TEL;WORK:5093277202 TEL;CELL:5099989234
TEL;FAX:5093272303
REV:20070109T144519Z
END:VCARD

Note that this Vcard example is simple in that it does not include any 3 part things like: TEL;WORK;VOICE: versus TEL;HOME;VOICE:, but my code handles getting what I want to support and makes it fairly easy for you to add any support you want. I chose not to support picture type info. Here is the overview code for reading into a set of controls:

 Private Sub FillControlsFromEmailVcf(ByVal msg As EmailEntity)
  Dim fullpath As String = msg.Attachment.Fullpath
  Dim areas() As String = Util.FileToStr(fullpath).Split(Cr)
  email1.Text = Info("EMAIL", areas, False)
  Dim names() As String = Info("N", areas, True).Split(";")
  If names.Length >= 1 Then
    lastname.Text = names(0)
  End If
  If names.Length >= 2 Then
    firstname.Text = names(1)
  End If
  If names.Length >= 3 Then
    middlename.Text = names(2)
  End If
  Dim addrs() As String = Info("ADR;HOME", areas, True).Split(";")
  If addrs.Length > 5 Then
    Me.address.Text = addrs(2)
    Me.city.Text = addrs(3)
    Me.state.Text = addrs(4)
    Me.zipsuffix.Text = addrs(5)
    Me.countryother.Text = addrs(6)
  End If
  Me.comp.Text = Info("ORG", areas, False)
  Dim caddrs() As String = Info("ADR;WORK", areas, True).Split(";")
  If caddrs.Length > 5 Then
    Me.baddress.Text = caddrs(2)
    Me.bcity.Text = caddrs(3)
    Me.bstate.Text = caddrs(4)
    Me.bzipsuffix.Text = caddrs(5)
    Me.bcountryother.Text = caddrs(6)
  End If
  Me.webaddr.Text = Info("URL", areas, False)
  Dim homePhone As String = Info("TEL;HOME", areas, False)
  homePhone = homePhone.Replace("(", "").Replace(")", "").Replace("-", "").Replace("+", "")
  If homePhone.Length >= 10 Then
    home1.Text = homePhone.Substring(0, 3)
    home2.Text = homePhone.Substring(3, 3)
    home3.Text = homePhone.Substring(6, 4)
    Try
      home4.Text = homePhone.Substring(10)
    Catch ex As Exception
    End Try
  End If
  Dim workPhone As String = Info("TEL;WORK", areas, False)
  workPhone = workPhone.Replace("(", "").Replace(")", "").Replace("-", "").Replace("+", "")
  If workPhone.Length >= 10 Then
    work1.Text = workPhone.Substring(0, 3)
    work2.Text = workPhone.Substring(3, 3)
    work3.Text = workPhone.Substring(6, 4)
    Try
      work4.Text = workPhone.Substring(10)
    Catch ex As Exception
    End Try
End If
  Dim cellPhone As String = Info("TEL;CELL", areas, False)
  cellPhone = cellPhone.Replace("(", "").Replace(")", "").Replace("-", "").Replace("+", "")
  If cellPhone.Length >= 10 Then
    cell1.Text = cellPhone.Substring(0, 3)
    cell2.Text = cellPhone.Substring(3, 3)
    cell3.Text = cellPhone.Substring(6, 4)
    Try
      cell4.Text = cellPhone.Substring(10)
    Catch ex As Exception
    End Try
  End If
  Dim faxPhone As String = Info("TEL;FAX", areas, False)
  faxPhone = faxPhone.Replace("(", "").Replace(")", "").Replace("-", "").Replace("+", "")
  If faxPhone.Length >= 10 Then
    fax1.Text = faxPhone.Substring(0, 3)
    fax2.Text = faxPhone.Substring(3, 3)
    fax3.Text = faxPhone.Substring(6, 4)
  Try
    fax4.Text = faxPhone.Substring(10)
  Catch ex As Exception
  End Try
  End If
Me.p1.Checked = True
End Sub
 

There are basically 2 types of lines in the file. Lines that hold one info item and lines that hold many items, so the input for the function that grabs a line has a boolean parameter for these different cases since in one case you don't want to strip semi-colons because you are going to split on the semi-colons. The code that gets the lines follows:

Private Function Info(ByVal head As String, ByVal areas() As String, ByVal split As Boolean) As String
  Dim result As String = String.Empty
  For Each area As String In areas
    area = area.Replace(Lf, "")
    If area.StartsWith(head) Then
      Dim colonPos As Integer = area.IndexOf(":")
      result = area.Substring(colonPos + 1)
      Exit For
    End If
  Next
  If Not split Then
    result = result.Replace(";", "")
  End If
  Return result
End Function
 

Send mail to Computer Consulting with questions or comments about this web site.
Last modified: January 10, 2007