little2do
asked on
Add a new contact to a public folder
Using the below code i am able to add a contact to MY personal address book.
'Ref Outlook 9.0 obj Lib
Set myOlApp = CreateObject("Outlook.Appl ication")
Set myNameSpace = myOlApp.GetNamespace("MAPI ")
Set myAddressList = myNameSpace.AddressLists ("Personal Address Book")
Set myAddressentries = myAddressList.AddressEntri es
Set newEntry = myAddressentries.Add("SMTP ")
With newEntry
.Address = "Address"
.Name = "Name"
.Update
End With
How do i do the same to a public Contacts folder in Public Folders - All Public Folders - Test?
'Ref Outlook 9.0 obj Lib
Set myOlApp = CreateObject("Outlook.Appl
Set myNameSpace = myOlApp.GetNamespace("MAPI
Set myAddressList = myNameSpace.AddressLists ("Personal Address Book")
Set myAddressentries = myAddressList.AddressEntri
Set newEntry = myAddressentries.Add("SMTP
With newEntry
.Address = "Address"
.Name = "Name"
.Update
End With
How do i do the same to a public Contacts folder in Public Folders - All Public Folders - Test?
ASKER
Thanks for the comment, unfortunately i am not sure what the syntax is to use use the Set myAddressList = myNameSpace.AddressLists(" Personal Address Book"). As i said i am trying to access a public folder and when i replace the "Personal Address Book" with the relevant folders name I get a can't be found message?
Any thoughts?
Any thoughts?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks kevotheclone - spot on.
ASKER
kevotheclone, if you get this - can i do the above just using cdo and not ref outlook 9.0?
Sorry I haven't responded sooner, but I've been really busy both at work and at home...
Although you can access a Folder using CDO and its Messages collection and a specific Item of the Messages collection, it doesn't look like you can work with Contact items. The CDO help file I have states that the Item property "...returns a single AppointmentItem, GroupHeader, MeetingItem, or Message object from the Messages collection." It doesn't say anything about Contact type of items, so maybe it will work, but maybe it won't.
There is a way via CDO to get to the user's default Contact folder, but this won't help you with a public Contacts folder:
Set objFolder = objSession.GetDefaultFolde r(CdoDefau ltFolderCo ntacts)
If you don't have it already you can download the CDO help file here:
http://www.cdolive.net/download/cdo.zip
Although you can access a Folder using CDO and its Messages collection and a specific Item of the Messages collection, it doesn't look like you can work with Contact items. The CDO help file I have states that the Item property "...returns a single AppointmentItem, GroupHeader, MeetingItem, or Message object from the Messages collection." It doesn't say anything about Contact type of items, so maybe it will work, but maybe it won't.
There is a way via CDO to get to the user's default Contact folder, but this won't help you with a public Contacts folder:
Set objFolder = objSession.GetDefaultFolde
If you don't have it already you can download the CDO help file here:
http://www.cdolive.net/download/cdo.zip
I'm still using Outlook 98 which uses the Outlook 8.0 Object Library.
I created two Sub procedures that you can call with the same exact parameters, so adding a entry to the Personal Address Book or the Contacts folder should be an easily interchangeable process.
I created a Driver() Sub to demo the AddContact() and AddPABEntry() Subs.
The key difference in the two is that the PAB is a special "thing" and the Contacts folder is a specialized MAPIFolder.
Be aware that a user can add many folders that can contain "Contact Items". The AddContact() sub as coded here will only add items to a users default "Contacts" folder, which I guess is the folder named "Contacts" capable of containing "Contact Items". If you need to add
"Contact Items" to a non-default Contact folder then you couldn't use the "myNameSpace.GetDefaultFol
I hope this solves your problem.
' Start of code...
Option Explicit
Sub Driver()
Dim myOlApp As Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Set myOlApp = CreateObject("Outlook.Appl
Set myNameSpace = myOlApp.GetNamespace("MAPI
AddContact myNameSpace, "little2do@experts-exchang
AddPABEntry myNameSpace, "little2do@experts-exchang
End Sub ' Driver()
Sub AddContact(myNameSpace As Outlook.NameSpace, strAddr As String, strName As String)
Dim fldrContacts As Outlook.mapiFolder
Dim ciNew As Outlook.ContactItem
Set fldrContacts = myNameSpace.GetDefaultFold
Set ciNew = myNameSpace.Application.Cr
With ciNew
.Email1Address = strAddr
.FullName = strName
.Save
End With ' ciNew
End Sub ' AddContact(myNameSpace As Outlook.NameSpace, strAddr As String, strName As String)
Sub AddPABEntry(myNameSpace As Outlook.NameSpace, strAddr As String, strName As String)
Dim myAddressList As Outlook.AddressList
Dim myAddressEntries As Outlook.AddressEntries
Dim newEntry As Outlook.AddressEntry
Set myAddressList = myNameSpace.AddressLists("
Set myAddressEntries = myAddressList.AddressEntri
Set newEntry = myAddressEntries.Add("SMTP
With newEntry
.Address = strAddr
.Name = strName
.Update
End With ' newEntry
End Sub ' AddPABEntry(myNameSpace As Outlook.NameSpace, strAddr As String, strName As String)
' End of code...