This is how I do it. Create a CContact class with getters and setters for these properties.
Private mlContactID As Long Private msLastName As String Private msFirstName As String Private msJobTitle As String Private msCompany As String Private msDepartment As String Private msEmail As String Private msBusinessPhone As String Private msCellPhone As String Private msPager As String Private msFax As String
Create a CContacts class to hold all instances of CContact. In CContacts, create a FillFromRange method to load all contacts.
Public Sub FillFromRange(rRng As Range) Dim vaValues As Variant Dim i As Long Dim clsContact As CContact vaValues = rRng.Value For i = LBound(vaValues, 1) To UBound(vaValues, 1) Set clsContact = New CContact With clsContact .ContactID = vaValues(i, 1) .LastName = vaValues(i, 2) .FirstName = vaValues(i, 3) .JobTitle = vaValues(i, 4) .Company = vaValues(i, 5) .Department = vaValues(i, 6) .Email = vaValues(i, 7) .BusinessPhone = vaValues(i, 8) .CellPhone = vaValues(i, 9) .Pager = vaValues(i, 10) .Fax = vaValues(i, 11) End With Me.Add clsContact Next i End Sub
Create procedures to populate classes like
Public Sub Auto_Open() Initialize End Sub Public Sub Initialize() Set gclsContacts = New CContacts gclsContacts.FillFromRange Sheet1.Range("C6").CurrentRegion End Sub
In this example, I am using a double-click event. When you double-click on a contact, a vcard is created. You will need to change to use the buttons. Get the TopLeftCell property of the button that was clicked to define the row.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean) Dim lContactID As Long lContactID = Me.Cells(Target.Row, 3).Value If gclsContacts Is Nothing Then Initialize If lContactID <> 0 Then gclsContacts.Contact(CStr(lContactID)).CreateVCardFile End If End Sub
Gets the identifier from column C and calls the CreateVCardFile method to write the file.
Public Sub CreateVCardFile() Dim sFile As String, lFile As Long Dim aOutput(1 To 12) As String lFile = FreeFile sFile = ThisWorkbook.Path & Application.PathSeparator & Me.VCardFileName Open sFile For Output As lFile aOutput(1) = gsBEGIN aOutput(2) = gsLASTNAME & Me.LastName aOutput(3) = gsFIRSTNAME & Me.FirstName aOutput(4) = gsTITLE & Me.JobTitle aOutput(5) = gsCOMPANY & Me.Company aOutput(6) = gsDEPARTMENT & Me.Department aOutput(7) = gsEMAIL & Me.Email aOutput(8) = gsBUSINESSPHONE & Me.BusinessPhone aOutput(9) = gsCELLPHONE & Me.CellPhone aOutput(10) = gsPAGER & Me.Pager aOutput(11) = gsFAX & Me.Fax aOutput(12) = gsEND Print #lFile, Join(aOutput, vbNewLine) Close lFile End Sub
It is just building a line and writing to a file. This example does not apply to the VCard specification, so you will have to develop these details. For this method, you will need some constants and a property that creates the file name.
Public Const gsBEGIN As String = "BEGIN:VCARD VERSSION: 3.0" Public Const gsEND As String = "END" Public Const gsLASTNAME As String = "N1;" Public Const gsFIRSTNAME As String = "N2;" Public Const gsTITLE As String = "TITLE;" Public Const gsCOMPANY As String = "ORG1;" Public Const gsDEPARTMENT As String = "ORG2;" Public Const gsEMAIL As String = "EMAIL,TYPE=WORK;" Public Const gsBUSINESSPHONE As String = "TEL,TYPE=WORK;" Public Const gsCELLPHONE As String = "TEL,TYPE=CELL;" Public Const gsPAGER As String = "TEL,TYPE=PAGER;" Public Const gsFAX As String = "TEL,TYPE=WORK,TYPE=FAX;"
And the file name property
Public Property Get VCardFileName() As String VCardFileName = Me.LastName & "_" & Me.FirstName & ".vcf" End Property
You can see the omitted details and how they work together by downloading this file.
http://dailydoseofexcel.com/excel/VCardCreator.zip