Link to home
Start Free TrialLog in
Avatar of karelke
karelkeFlag for Belgium

asked on

import contacts from csv file into outlook using vba

Dear Experts,


I need your urgent help about vba coding. I want to import csv file into outlook using vba. I found code, but it doesn' work. It creates only subfolder in outlook "contacts" and then DL's, but it doesn't make any contacts or populate the DL's.
Can someone help me to run this code. The code has to create contacts and next popluate the DL.

regards,

Karel
code.txt
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

There is a line which starts  - On Error Resume Next near the beginning of the code.

I suggest that you comment that line out so that errors are reported instead of being ignored.
Avatar of karelke

ASKER

Hi Grahamskan,

I did it gives error on the next lines (see below):

'On Error Resume Next 'this will stop users receiving errors, if you would like to debug before distributing, remove this

    Set objFolder = myNameSpace.GetDefaultFolder(olFolderContacts).Folders(Name) 'sets the place the contacts will go to

    objFolder.ShowAsOutlookAB = False 'takes the old version of the folder out of the active contacts list

    objFolder.Delete 'deletes the old folder

 

    If Not objFolder Is Nothing Then

        objFolder.ShowAsOutlookAB = False

        objFolder.Delete

        F.Folders.Add Name

    End If

 

    F.Folders.Add Name 'creates new folder in desired location

Avatar of karelke

ASKER

error:run-time error -2147221233 (8004010f)
the operation failed. an object could not be found
-------------------------

regards,

Karel
Does the error tell you what was wrong?

What is the message?
Sorry. Cross-posted.

The code does not check for the existence of the folder before trying to set it, so a quick solution is to restore the On error, but to turn off Error handling handling after the delete attempt.

On Error Resume Next 'this will stop users receiving errors, if you would like to debug before distributing, remove this
    Set objFolder = myNameSpace.GetDefaultFolder(olFolderContacts).Folders(Name) 'sets the place the contacts will go to
    objFolder.ShowAsOutlookAB = False 'takes the old version of the folder out of the active contacts list
    objFolder.Delete 'deletes the old folder
On Error GoTo 0

Open in new window

Avatar of karelke

ASKER

Hi GrahamSkan,

it gives still an error :(.

error.JPG
Avatar of karelke

ASKER

When I delete the contact folder manaully. it gives this error msg:
line: Set Wkb = ObjExcel.Workbooks.Open(FileName:=Path & " \ " & FName) 'opens the csv file as the active workbook
error.-2JPG.JPG
Are you the administrator?
Avatar of karelke

ASKER

I have full admin rights
Avatar of karelke

ASKER

Hi,

I modified a bit and got now this error: Runtime Error 9: Subscript out of range


objFolder.ShowAsOutlookAB = True ' ticks box to see folder content items as contacts

Path = "C:\import" ' path where csv file is saved to

FName = "contacts.csv" 'name of csv file

 

Set ObjExcel = New Excel.Application ' opens excel (will not be visible to the user)

Set Wkb = ObjExcel.Workbooks.Open(FileName:=Path & "\" & FName) 'opens the csv file as the active workbook

'line below gives error
Set WS = Wkb.Sheets("sheet1") ' name of the sheet in the workbook the contacts are stored in (default = "sheet1")
That means that you there isn't a sheet called "sheet1" in the workbook.
If you're not sure of the name, you can use the numerical index instead.

Set WS = Wkb.Sheets(1)

Otherwise, give it the actual name that you are using, e.g. :

Set WS = Wkb.Sheets("MySheet")


Avatar of karelke

ASKER

Hi GrahamSkan,

I'm lost now :(. It adds now, not all of contacts, few in address book in wrong way.
this are my headers: "First Name","Last Name","Business Phone","Mobile Phone","E-mail Address"
And in the new created contats it gives like this: full name= first name + business phone nr
Avatar of karelke

ASKER

I found error:)
That's great. Well done.
Avatar of karelke

ASKER

Only problems is now to populate DL. That doesn't work.
Avatar of karelke

ASKER

I have only one question. How can i change the code so that all contacts will be created directly in root folder in stead of in subfolder please?
ASKER CERTIFIED SOLUTION
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of karelke

ASKER

Sorry for late reply. Thanks to you the script works to import. Thanks really lot :D. But can you only help to avoid duplicates please during the update. I saw when I run the script twice it creates all the contacts twice in Folder contacts :(.