这就是我将如何做到的。使用这些属性的 getter 和 setter 创建一个名为 CContact 的类。
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
创建一个 CContacts 类来保存所有 CContact 实例。在 CContacts 中,创建一个 FillFromRange 方法来加载所有联系人。
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
创建过程来填充类,像这样
Public Sub Auto_Open()
Initialize
End Sub
Public Sub Initialize()
Set gclsContacts = New CContacts
gclsContacts.FillFromRange Sheet1.Range("C6").CurrentRegion
End Sub
对于此示例,我使用的是双击事件。双击联系人时,将创建电子名片。您需要修改以使用按钮。获取单击以确定行的按钮的 TopLeftCell 属性。
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
这从 C 列获取 ID 并调用 CreateVCardFile 方法来写出文件。
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
那只是构建一个字符串并写入文件。此示例不符合 VCard 规范,因此您必须弄清楚这些细节。对于此方法,您需要一些常量和一个创建文件名的属性。
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;"
和文件名属性
Public Property Get VCardFileName() As String
VCardFileName = Me.LastName & "_" & Me.FirstName & ".vcf"
End Property
您可以通过下载此文件查看省略的详细信息以及它们如何协同工作。
http://dailydoseofexcel.com/excel/VCardCreator.zip