Import Contacts into Outlook 2003 from CSV

Currently I have a CSV file, non-standard headers sadly (as in ADDR1 instead of Address1), and I need it to import each record into Outlook as a contact.

I can do it manually but I want to do it using VB.NET (Visual Studio 2005 if it matters).

I know there are better ways but what I was considering was to read the CSV into an array and split the records up. Then put that into Outlook.


You would just Loop this for each record I assume:
        Dim olApp As Outlook.Application
        olApp = CreateObject("Outlook.Application")
        Dim olNs As Outlook.NameSpace
        olNs = olApp.GetNamespace("MAPI")
        olNs.Logon()
        Dim olItem As Outlook.ContactItem
        olItem = olApp.CreateItem(olContactItem)
        With olItem
            .FullName = Name
            .Birthday = BDay
            .CompanyName = Company
            .HomeTelephoneNumber = Phone
            .Email1Address = Email
            .JobTitle = Job
            .HomeAddress = ADDR
        End With
        olItem.Save()


The problem I have is loading the CSV and splitting it up into variables that I can loop through.
Also the actual data I need is only Name, Company, and Phone number. The rest is just a bonus. I'm eventually going to schedule this to happen weekly, no Forms needed just a Console App.
BiSHGoDAsked:
Who is Participating?
 
GranModCommented:
PAQed with no points refunded (of 350)

GranMod
Community Support Moderator
0
 
bruintjeCommented:
Hi BiSHGoD,
----------

a few weeks ago i wrote a few scripts on this site doing this and a bit more, but they are VBS so you can add them to the task scheduler

for the fun of it [well almost] this is your console vb.net version based on c:\contacts.csv
you don't need the headers but add the fields based on the column in the csv
didn't add robust error handling only a simple try catch and a finally to clean up
if you test it on a copy of your data first, and compile it into an exe you can add it to the task scheduler for automatic runs

---
Imports System.IO

Module Module1

  Sub Main()
    Dim objNS As Object, oOlFolder As Object, objOutlook As Object
    Dim myItems As Object, objContact As Object

    Const olFolderContacts = 10

    Try

      objOutlook = CreateObject("Outlook.Application")
      objNS = objOutlook.GetNamespace("MAPI")
      oOlFolder = objNS.GetDefaultFolder(olFolderContacts) ' this will add them to the default contacts
      myItems = oOlFolder.Items

      Dim strContact() As String
      Dim strLine As String
      If System.IO.File.Exists("c:\contacts.csv") Then
        Dim objstreamreader As StreamReader = System.IO.File.OpenText("test.txt")
        strLine = objstreamreader.ReadLine
        Do Until strLine Is Nothing

          strContact = strLine.Split(",")
          objContact = myItems.Add("IPM.Contact.CTXContacts")
          objContact.FullName = strContact(0)
          objContact.CompanyName = strContact(1)
          objContact.Email1Address = strContact(2)
          objContact.BusinessTelephoneNumber = strContact(3)
          objContact.HomeTelephoneNumber = strContact(4)
          objContact.BusinessFaxNumber = strContact(5)
          objContact.MobileTelephoneNumber = strContact(6)
          objContact.BusinessAddress = strContact(7)
          objContact.Department = strContact(8)
          objContact.OfficeLocation = strContact(9)
          objContact.ManagerName = strContact(10)
          objContact.Body = strContact(11)
          objContact.Save()

          strLine = objstreamreader.ReadLine()
        Loop
        objstreamreader.Close()
      End If

    Catch ex As Exception
      MsgBox(ex.Message)

    Finally
      myItems = Nothing
      oOlFolder = Nothing
      objNS = Nothing
      objOutlook = Nothing
      objContact = Nothing
    End Try

  End Sub

End Module
---

----------
bruintje
share what you know, learn what you don't
0
 
BiSHGoDAuthor Commented:
Sorry, been a bit busy that I forgot to respond.

That code snippet didn't work in my environment. I already have some vb.net code, above, and would have liked to build on that.

Consequently I did figure out how to code it myself in VB.NET.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.