• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1760
  • Last Modified:

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
0
karelke
Asked:
karelke
  • 11
  • 7
1 Solution
 
GrahamSkanCommented:
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.
0
 
karelkeAuthor Commented:
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

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

regards,

Karel
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
GrahamSkanCommented:
Does the error tell you what was wrong?

What is the message?
0
 
GrahamSkanCommented:
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

0
 
karelkeAuthor Commented:
Hi GrahamSkan,

it gives still an error :(.

error.JPG
0
 
karelkeAuthor Commented:
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
0
 
GrahamSkanCommented:
Are you the administrator?
0
 
karelkeAuthor Commented:
I have full admin rights
0
 
karelkeAuthor Commented:
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")
0
 
GrahamSkanCommented:
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")


0
 
karelkeAuthor Commented:
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
0
 
karelkeAuthor Commented:
I found error:)
0
 
GrahamSkanCommented:
That's great. Well done.
0
 
karelkeAuthor Commented:
Only problems is now to populate DL. That doesn't work.
0
 
karelkeAuthor Commented:
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?
0
 
GrahamSkanCommented:
I think that you just need to omit the new folder creation.
'...
Set olApp = CreateObject("Outlook.Application")
Set myNameSpace = olApp.GetNamespace("MAPI")
lastline = False
i = 1
 
'code deleted from here
 
If Name = "Contacts" Or IsMissing(Name) Or Name = "" Then
Set objFolder = myNameSpace.GetDefaultFolder(olFolderContacts)
Else
Set objFolder = myNameSpace.GetDefaultFolder(olFolderContacts).Folders(Name)
End If
'...

Open in new window

0
 
karelkeAuthor Commented:
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 :(.

0

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

  • 11
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now