4

我正在考虑创建一个 excel 文件,我将在其中手动填写多个联系人信息,以便我可以将联系人(一个接一个)或全部导出到指定目录中的单个 vcf 文件。我想最好的方法是通过 VBA,但我不是很了解,需要一点推动。

请参阅下面带有联系人字段的 excel 文件的屏幕截图。

在此处输入图像描述

任何指导将不胜感激。

好的,所以我最初是从解决将每一行导出到单个 vcard 的问题开始的。我遵循以下策略:

  1. 创建临时新工作表 (tmp)
  2. 粘贴标题:BEGIN:VCARD 版本:3.0
  3. 根据我的图像复制粘贴第 4 行,以便它包含 VCARD 的 ID 以及我尝试导出的行(在第一种情况下为第 6 行)。我将它们粘贴到工作表 tmp。

我在这个阶段卡住了,因为 vcard 用于某些字段的方式是用“;”分隔它们。他们处于不同的位置。我不知道如何通过查看第 4 行的字段在 VBA 中生成这些。即:N1 和 N2 应该为我创建以下行:N:Stuart;Carol。ADR 领域也是如此。

一旦生成了完整的代码,我就有了生成 VCARD 文件的代码。

在这一点上的任何帮助将不胜感激。

4

3 回答 3

2

这就是我将如何做到的。使用这些属性的 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

于 2012-11-08T21:02:45.383 回答
1

EDIT: Had you admitted you have practically no useful knowledge of the Visual Basic language, and in fact you needed a SOLUTION, not merely a hint or "push" as you call it. A solution is not to be confused with simple "help". I would have come up with a much shorter code to accomplish the goal of your excel worksheet, exporting it to whatever format, provided you mentioned the specific result you intended (and I mean an example of a vCard code) in a very much shorter time and without requiring any more help from your side. But you're acting like a professional who just needs a push... if there is such a thing. Again, this is not about pride, nor (I hope) am I being offensive, I'm just criticizing the act to drop a hint regarding the limits of this particular site.

Your question is rather general, and I think the purpose of this website is to find answers to specific issues and not giving solutions to problems of the complexity of an application. This being my first try, maybe I'm wrong, but I this question seems to be eligible for a down vote per the description "this question does not show any research effort. it is unclear or not useful".

However, if all you need is a push indeed, then you can find information on the VCard file format on Wikipedia (with examples) - it says the latest version of the format is XML, so it should be easy if you know how to manipulate strings in Visual Basic and access Excel cell contents from a script. http://en.wikipedia.org/wiki/VCard

If you don't, you can learn by practice on a per-needed basis at websites such as http://www.excel-vba-easy.com/

Try, and return with a more specific issue. I would've simply commented on the question instead of "answering", but I haven't found a way to that.

于 2012-11-08T18:10:15.557 回答
0

我有一个样本 excel 表和与之对应的 VBA 代码。 样品Excel表

这是将其转换为 vcf 类型的相应 VBA 代码。

Private Sub Create_VCF()
'Open a File in Specific Path in Output or Append mode
Dim FileNum As Integer
Dim iRow As Integer
Dim FirstName As String
Dim LastName As String
Dim FullName As String
Dim EmailAddress As String
Dim PhoneHome As String
Dim PhoneWork As String
Dim Organization As String
Dim JobTitle As String


iRow = 3
FileNum = FreeFile
OutFilePath = "C:\output.VCF"
Open OutFilePath For Output As FileNum

'Loop through Excel Sheet each row and write it to VCF File
While VBA.Trim(Sheets("Sheet1").Cells(iRow, 1)) <> ""



    FirstName = VBA.Trim(Sheets("Sheet1").Cells(iRow, 1))
    LastName = VBA.Trim(Sheets("Sheet1").Cells(iRow, 2))
    FullName = VBA.Trim(Sheets("Sheet1").Cells(iRow, 3))
    EmailAddress = VBA.Trim(Sheets("Sheet1").Cells(iRow, 4))
    PhoneWork = VBA.Trim(Sheets("Sheet1").Cells(iRow, 5))
    PhoneHome = VBA.Trim(Sheets("Sheet1").Cells(iRow, 6))
    Organization = VBA.Trim(Sheets("Sheet1").Cells(iRow, 7))
    JobTitle = VBA.Trim(Sheets("Sheet1").Cells(iRow, 8))

    Print #FileNum, "BEGIN:VCARD"
    Print #FileNum, "VERSION:3.0"

    Print #FileNum, "N:" & FirstName & ";" & LastName & ";;;"
    Print #FileNum, "FN:" & FullName
    Print #FileNum, "ORG:" & Organization
    Print #FileNum, "TITLE:" & JobTitle
    Print #FileNum, "TEL;TYPE=HOME,VOICE:" & PhoneHome
    Print #FileNum, "TEL;TYPE=WORK,VOICE:" & PhoneWork
    Print #FileNum, "EMAIL:" & EmailAddress

    Print #FileNum, "END:VCARD"
    iRow = iRow + 1
Wend

'Close The File
Close #FileNum
MsgBox "Contacts Converted to Saved To: " & OutFilePath & " 
End Sub

谢谢,我希望这会有所帮助。

于 2019-06-09T12:43:19.757 回答