2015-01-13 92 views
0

我已经得到了这段代码,它似乎通过联系人列表循环,但它不会创建Outlook联系人,也不会产生错误。有任何想法吗?VB.net从CSV文件创建联系人

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 
    Dim afile As FileIO.TextFieldParser = New FileIO.TextFieldParser("c:\contacts.csv") 
    Dim CurrentRecord As String() ' this array will hold each line of data 
    afile.TextFieldType = FileIO.FieldType.Delimited 
    afile.Delimiters = New String() {vbTab} 
    afile.HasFieldsEnclosedInQuotes = True 


    Dim oApp As Outlook.Application 
    oApp = CreateObject("Outlook.Application") 

    Dim oNs As Outlook.NameSpace 
    oNs = oApp.GetNamespace("MAPI") 
    oNs.Logon() 

    Dim oItem As Outlook.ContactItem 
    oItem = oApp.CreateItem(OlItemType.olContactItem) 

    ' parse the actual file 
    Do While Not afile.EndOfData 
     Try 
      CurrentRecord = afile.ReadFields 

      With oItem 
       .FirstName = CurrentRecord(0) 
       .LastName = CurrentRecord(1) 
       End With 

      oItem.Save() 
     Catch ex As FileIO.MalformedLineException 
      Stop 
     End Try 
    Loop 
    MsgBox("Complete") 
End Sub 

不知道这是否是最好的方法,所以我愿意提供建议。

回答

0

不知道这是否是问题的全部,但你应该创建循环内每个联系人(是的,我更喜欢“做,直到”在“做虽然不是”):

Do Until afile.EndOfData 
    Try 
     CurrentRecord = afile.ReadFields 
     oItem = oApp.CreateItem(OlItemType.olContactItem) 
     With oItem 
      .FirstName = CurrentRecord(0) 
      .LastName = CurrentRecord(1) 
      .Save() 
     End With    
    Catch ex As FileIO.MalformedLineException 
     Stop 
    End Try 
Loop