Solved

Add a new contact to a public folder

Posted on 2002-07-12
6
404 Views
Last Modified: 2008-02-01
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.Application")
    Set myNameSpace = myOlApp.GetNamespace("MAPI")
    Set myAddressList = myNameSpace.AddressLists ("Personal Address Book")
    Set myAddressentries = myAddressList.AddressEntries
   
    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?
0
Comment
Question by:little2do
  • 3
  • 3
6 Comments
 
LVL 2

Expert Comment

by:kevotheclone
ID: 7149676
Here's an example that should work.
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.GetDefaultFolder(olFolderContacts)" method.


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.Application")
  Set myNameSpace = myOlApp.GetNamespace("MAPI")
 
  AddContact myNameSpace, "little2do@experts-exchange.com", "little2do"
 
  AddPABEntry myNameSpace, "little2do@experts-exchange.com", "little2do"
 
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.GetDefaultFolder(olFolderContacts)
  Set ciNew = myNameSpace.Application.CreateItem(olContactItem)
 
  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("Personal Address Book")
  Set myAddressEntries = myAddressList.AddressEntries
 
  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...
0
 

Author Comment

by:little2do
ID: 7149703
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?

0
 
LVL 2

Accepted Solution

by:
kevotheclone earned 100 total points
ID: 7149849
Opps sorry I forgot that last sentence....

Here's another Sub that uses the same parameters as the previous two Sub but this time it will add a "Contact Item" to a public folder capable of accepting Contact Items.

You need to have a variable that points to the MAPIFolder where you want to save the item.  Then you use the Add method of the MAPIFolder's Items collection to create a new blank item (this technique should work for any MAPIFolder regardless of type or location).

Once you have the a new blank item of the correct type you fill out it's properties and save it, just like the AddressEntry.

The annoying part of the code is getting to a folder that is Setting a variable to the MAPIFolder object.  You need to use the Namespace.Folders.Item method repeatedly to navigate down the folder tree until you get the folder you want.  This could be one log line of code (as in my example) or it could be broken up into multiple statements.

I've tried to pass it a path to the folder but I can't get it to work:

"Public folders\this\that"
"Public folders/this/that"
"outlook://Public folders/this/that"

If you need a lot of flexibility then you could create a Function that would accept a folder path as a string argument, that would parse the path and return a MAPIFolder object.

' Start of code...
Sub AddPublicContact(myNameSpace As Outlook.NameSpace, strAddr As String, strName As String)
  Dim fldrContacts As Outlook.mapiFolder
  Dim ciNew        As Outlook.ContactItem
 
  Set fldrContacts = myNameSpace.Folders.Item("Public Folders").Folders.Item("DomainName").Folders.Item("UnitName").Folders.Item("PublicContacts")

  Set ciNew = fldrContacts.Items.Add(olContactItem)
 
  With ciNew
    .Email1Address = strAddr
    .FullName = strName
    .Save
  End With ' ciNew
 
End Sub ' AddContact(myNameSpace As Outlook.NameSpace, strAddr As String, strName As String)
' End of code...
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:little2do
ID: 7153675
Thanks kevotheclone - spot on.
0
 

Author Comment

by:little2do
ID: 7157461
kevotheclone, if you get this - can i do the above just using cdo and not ref outlook 9.0?
0
 
LVL 2

Expert Comment

by:kevotheclone
ID: 7180963
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.GetDefaultFolder(CdoDefaultFolderContacts)

If you don't have it already you can download the CDO help file here:
http://www.cdolive.net/download/cdo.zip
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now