2015-01-13 125 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