Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add Outlook contacts via VB6

Status
Not open for further replies.

L0stAngel

Programmer
May 5, 2005
58
US
Hey guys, i've gotta problem here and was wondering if theres any other VB Gurus here.

What I am trying to do is, have a VB app read contacts from a .csv file, and import them into Outlook 2000. I can get this to happen...but what i'm having trouble on is having them add to a specific contact folder (address list) in outlook. When the below code is executed, it simply adds it to the root 'Contact' folder....Below is the code I have so far:

Dim ol As Outlook.Application
Dim ns As Outlook.NameSpace
Dim fl As Outlook.Folders
Dim itmContact As Outlook.ContactItem

' grab Outlook
Set ol = New Outlook.Application

' get MAPI reference
Set ns = ol.GetNamespace("MAPI")
' Create new Contact item
Set itmContact = ol.CreateItem(olContactItem)

' Setup Contact information...
With itmContact
.FullName = "James Smith"
.Anniversary = "09/15/1997"

' saving b-day info creates info in the calendar
.Birthday = "9/15/1975"

.CompanyName = "Microsoft"
.HomeTelephoneNumber = "704-555-8888"
.Email1Address = "someone@microsoft.com"
.JobTitle = "Developer"
.HomeAddress = "111 Main St." & vbCr & "Charlotte, NC 28226"

End With

' Save Contact...
itmContact.Save

Set ol = Nothing
Set ns = Nothing
Set itmContact = Nothing

MsgBox "Done.
 
This should be about it (haven't actually got VB or Outlook on this machine, so have not tested the code I've written - howevere the core difference is that it works by adding an item to a folder, assumed to be of Type Contact, rather than the item creation method that you used)
Code:
[blue]	Dim ol As Outlook.Application
	Dim ns As Outlook.NameSpace
	Dim myFolder As Outlook.MAPIFolder
	Dim myNewFolder As Outlook.MAPIFolder
	Dim itmContact As Outlook.ContactItem

	Set ol = New Outlook.Application	
	Set ns = ol.GetNamespace("MAPI")

	Set myFolder = ns.GetDefaultFolder(olFolderContacts)	
	Set myNewFolder = myFolder.Folders("A Contacts Subfolder") ' or whatever your folder is called, assuming it is in the default Contacts folder
	Set itmContact = myNewFolder.Items.Add

	 With itmContact
      		.FullName = "James Smith"
      		.Anniversary = "09/15/1997"
      
     		 ' saving b-day info creates info in the calendar
      		.Birthday = "9/15/1975"
      
      		.CompanyName = "Microsoft"
      		.HomeTelephoneNumber = "704-555-8888"
      		.Email1Address = "someone@microsoft.com"
      		.JobTitle = "Developer"
      		.HomeAddress = "111 Main St." & vbCr & "Charlotte, NC 28226"       
	End With[/blue]
 
Thank you so much for this reply, it worked great!! My last question is, how can I Delete and Recreate a specific Contact Subfolder?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top