Session.AddressBook doesnt find my address lists.

G

Guest

Hello. I am writing a C# program that uses CDO 1.21 to access the outlook addressbook as a contact management system

The following code gives me the correct dialog box, but the "Show Names from the:" drop down list box is empty

I opened the correct default profile (i have only 1 profile) and I should see Global Address List (from the exchange server), and at least 1 Contact folder

try

Object[] argsAddressBook = new Object[9]
argsAddressBook[0] = Missing.Value
argsAddressBook[1] = Missing.Value
argsAddressBook[2] = Missing.Value
argsAddressBook[3] = Missing.Value
argsAddressBook[4] = Missing.Value
argsAddressBook[5] = Missing.Value
argsAddressBook[6] = Missing.Value
argsAddressBook[7] = Missing.Value
argsAddressBook[8] = Missing.Value

MAPI.Session oSession = new MAPI.Session()

argsLogon[0] = Missing.Value; // ProfileNam
argsLogon[1] = Missing.Value; // ProfilePasswor
argsLogon[2] = Missing.Value; // ShowDialo
argsLogon[3] = Missing.Value; // NewSessio
argsLogon[4] = Missing.Value; // ParentWindo
argsLogon[5] = Missing.Value; // NoMai
argsLogon[6] = Missing.Value; // ProfileInf
oSession.GetType().InvokeMember("Logon", BindingFlags.InvokeMethod, null, oSession, argsLogon)

MAPI.Recipients oRecipients
(MAPI.Recipients)oSession.GetType().InvokeMember("AddressBook", BindingFlags.Default |BindingFlags.GetProperty, null, oSession, argsAddressBook)



The last statement brings up the correct dialog box, but it doesnt let me choose any address lists from the drop down list box

Other techniques, such as Outlook Object Model can find my folders, but its too slow so I want to do it with CDO

I know CDO 1.21 is "unsupported" with .net and c#, but is there anything obvious I have forgotten or any other way to access the AddressBooks

If I use Session.GetFolder with the correct EntryID, I can get my Contacts folder and other folders directly, and I guess I could make my own dialog box and iterate through the addresses with Folder.Messages, but I would prefer to use the builtin GUI

Thanks a lot for any tips

Kurt
 
R

Russell Mangel

Try this:

Russell Mangel
Las Vegas, NV

// This routine, will loop through all AddressLists/AddressEntries
MAPI.SessionClass oSession = new MAPI.SessionClass();
oSession.Logon(System.Environment.UserName,
System.Reflection.Missing.Value, true, true,
System.Reflection.Missing.Value, false, System.Reflection.Missing.Value);

MAPI.AddressLists oAddressLists =
(MAPI.AddressLists)oSession.AddressLists;

for(int i = 1; i <= (int)oAddressLists.Count; i++)
{
// There can be several address lists, get first
MAPI.AddressList oAddressList =
(MAPI.AddressList)oAddressLists.get_Item(i);
// Print out each AddressList name
Console.WriteLine(oAddressList.Name);

MAPI.AddressEntries oAddressEntries =
(MAPI.AddressEntries)oAddressList.AddressEntries;
for(int j = 1; j <= (int)oAddressEntries.Count; j++)
{
MAPI.AddressEntry oAddressEntry =
(MAPI.AddressEntry)oAddressEntries.get_Item(j);
// Print out each AddressEntry name
Console.WriteLine(" " + oAddressEntry.Name);
}
}

// End Code
 
R

Russell Mangel

Heres an example that selects one addresslist, instead of looping through
all of them:

Russell Mangel
Las Vegas, NV

// This routine, will allow you to select one AddressList

MAPI.SessionClass oSession = new MAPI.SessionClass();
oSession.Logon(System.Environment.UserName,
System.Reflection.Missing.Value, true, true,
System.Reflection.Missing.Value, false, System.Reflection.Missing.Value);

// You can select one AddressList, by changing the last enumerated type
MAPI.AddressList oAddressList =
(MAPI.AddressList)oSession.GetAddressList(MAPI.CdoAddressListTypes.CdoAddres
sListPAB);

// If you are connected to Exchange Server, you can use Global Address
List
// MAPI.AddressList oAddressList =
(MAPI.AddressList)oSession.GetAddressList(MAPI.CdoAddressListTypes.CdoAddres
sListGAL);
Console.WriteLine(oAddressList.Name);
MAPI.AddressEntries oAddressEntries =
(MAPI.AddressEntries)oAddressList.AddressEntries;

for(int j = 1; j <= (int)oAddressEntries.Count; j++)
{
MAPI.AddressEntry oAddressEntry =
(MAPI.AddressEntry)oAddressEntries.get_Item(j);
// Print out each AddressEntry name
Console.WriteLine(" " + oAddressEntry.Name);
}
 
R

Russell Mangel

One more example for displaying Dialog box for Recipients

Russell Mangel
Las Vegas, NV

// This routine will display dialog box, for you to select Recipient.
MAPI.SessionClass oSession = new MAPI.SessionClass();
oSession.Logon(System.Environment.UserName,
System.Reflection.Missing.Value, true, true,
System.Reflection.Missing.Value, false, System.Reflection.Missing.Value);

MAPI.Recipients oRecipients = null;
MAPI.Recipient oRecipient = null;
MAPI.AddressEntry oAddressEntry = null;

// Display dialog box to select recipient(s)
// Watch the wrapping on this next line, everything needs to be on one
line.
oRecipients =
(MAPI.Recipients)oSession.AddressBook(System.Reflection.Missing.Value,
"Select Name", true, true, -1, "", "", "", 0);

for(int i = 1; i <= (int)oRecipients.Count; i++)
{
// Do what you need with Recipient, or AddressEntry
oRecipient = (MAPI.Recipient)oRecipients.get_Item(i);
oAddressEntry = (MAPI.AddressEntry)oRecipient.AddressEntry;

Console.WriteLine(oAddressEntry.Name);
}

oSession.Logoff();

// The following are the parameters for oSession.AddressBook() method

// 1st parameter, allows all AddressLists to be displayed
// 2nd parameter, string The Title bar text for the Dialog Box
// 3rd parameter, boolean, Allow one address to be selected at one
time.
// 4th parameter, boolean, Resolve all names
// 5th prameter, int -1 , 0, 1, 2, 3 Number of recipient list boxes to
display
// 6th parameter, string, Text for 1st button
// 7th parameter, string, Text for 2nd button
// 8th paramter, string, Text for 3rd button
// 9th parameter, int 0 = (Modal dialog box) Parent window handle
 
I

Integer Software

Hi Russel,

Thanks a lot for your suggestion. When I run the following code,
oAddressLists.Count = 0 and the outer loop iterates 0 times.

Additionally, if I add a oSession.Logoff() at the very end, it takes
exactly 60 seconds to timeout.

I shall now try out your other suggestions.

I am suspecting something might not be correctly set up here on my PC.

Thanks

Kurt
 
I

Integer Software

Thanks Russel.

I tried 3 different values of AddressListType in the GetAddressList call.

CdoAddressListPAB and GAL both cause an exception with error code E_FAIL.

If I try CdoAddressListTotal I get an exception with error code
E_INVALIDARG.

Strange. This is all on XP professional with latest service packs and
updates, office 2003 with all updates, visual studio 2003 .net. My test
folders reside on a windows 2000 server with exchange 2000 on it.
Outlook etc seems to work fine.

Ok a few more things to check...

Thanks

Kurt
 
I

Integer Software

Hmmm

Thanks Russell

But I get the same problem as I had before.

The AddressBook dialog box appears, but the drop down list box contains
no address lists :-(

Additionally, the logoff takes exactly 60 seconds. In other examples it
takes exactly 120 seconds which makes me think there is some minute long
timeout value specified somewhere....

I might have to try this program on another machine.

Thanks a lot,

Kurt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top