Create Contact in Folder/Delete Contact in Folder
Hey, awhile ago I posted here asking for help with outlook 2003 and my problems were resolved...however when I try working with outlook 2000...my code breaks. I have the following code for just adding a contact to outlook:
Code:
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 = "[email protected]"
.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."
That works, but what I would like to do is...upon clicking the button that executes that code, I would like it to find the Contact folder named '123'...clear the contacts from it....then add the contact above. When I try working with addresslists (like I did in 2k3), I get errors saying Type Mismatch.
Re: Create Contact in Folder/Delete Contact in Folder
Well you are not loggin on to the outlook profile/session that may be one part. You are using Early Binding that is specific to 2003 reference. For this you should be using Late Binding with no reference to Outlook.
See my faq item on Early and Late binding
http://vbforums.com/showthread.php?t=406640
Re: Create Contact in Folder/Delete Contact in Folder
I just looked, and that post speaks of sending a mail message...but not saving a contact...im sorry if I sound really stupid with this. The above code works fine...do you perhaps know the syntax to add it to a specific contact folder (instead of the root contact folder)?
Re: Create Contact in Folder/Delete Contact in Folder
Well I guess you didnt read it then as that was not about sending email but the process of Early binding vs Late binding and how it can help you to support multiple versions of Outlook. What is generating the type mismatch error?
Re: Create Contact in Folder/Delete Contact in Folder
I have the same problem:
i know, how to find, add, delete, update a contact-item, BUT:
i want to do all the operations NOT in the default contact folder, but in
my own contact folder named "sigiK" - so i first have to find this folder out from outlook - how can i do this ??
Re: Create Contact in Folder/Delete Contact in Folder
solved ... :)
finding myFolder:
Code:
Dim oNS As Outlook.NameSpace = oApp.GetNamespace("MAPI")
Dim oContacts As Outlook.MAPIFolder
oContacts = FindMyFolder(oNS.Folders)
Private Function FindMyFolder(ByVal F As Outlook.Folders) As Outlook.MAPIFolder
Dim wf As Outlook.MAPIFolder
Dim cf As Outlook.MAPIFolder
For Each wf In F
If wf.Name = "MyFolder" Then Return wf
If wf.Folders.Count > 0 Then
cf = FindMyFolder(wf.Folders)
If Not cf Is Nothing Then Return cf
End If
Next
Return Nothing
End Function
adding contact:
Code:
oContact = oContacts.Items.Add
With oContact
.LastName = ....
[.......]
.User1 = 5
.Save()
End With
finding contact:
Code:
oContact = oContacts.Items.Find("[User1] = '5'")
sorry, if code seems stupid, but i am a greenhorn ...