karelke
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
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
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.GetDefaultFold er(olFolde rContacts) .Folders(N ame) '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
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.GetDefaultFold
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
ASKER
error:run-time error -2147221233 (8004010f)
the operation failed. an object could not be found
-------------------------
regards,
Karel
the operation failed. an object could not be found
-------------------------
regards,
Karel
Does the error tell you what was wrong?
What is the message?
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.
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
ASKER
ASKER
When I delete the contact folder manaully. it gives this error msg:
line: Set Wkb = ObjExcel.Workbooks.Open(Fi leName:=Pa th & " \ " & FName) 'opens the csv file as the active workbook
error.-2JPG.JPG
line: Set Wkb = ObjExcel.Workbooks.Open(Fi
error.-2JPG.JPG
Are you the administrator?
ASKER
I have full admin rights
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(Fi leName:=Pa th & "\" & 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")
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(Fi
'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")
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")
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
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
ASKER
I found error:)
That's great. Well done.
ASKER
Only problems is now to populate DL. That doesn't work.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 :(.
I suggest that you comment that line out so that errors are reported instead of being ignored.