HOWTO: Access Contacts?

P

Peter

Hello,

Thanks for reviewing my question. I have followed the Microsoft Knowledge Base Article - 313802 and get exception when trying to access the first contact in the Outlook's contact list. I also created a VB program and copied the sample from the article and got the same exception. The following is a sample of my code:

Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace oNS = oApp.GetNamespace("mapi");
oNS.Logon(Missing.Value, Missing.Value, false, true);
Outlook.MAPIFolder cContacts = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);

Outlook.Items oItems = cContacts.Items;

Outlook.ContactItem oCt = (Outlook.ContactItem)oItems.GetFirst(); // EXCEPTION IS THROWN

It says it was a bad cast. Any ideas?

Many Thanks
Peter
 
E

Eusch

It seems that the folders in outlook don't just contain the items should would expect. When looping through all email messages in the inbox I had the same kind of problem. In vb use a variable of type object and check it's type before doing something with it. A piece of code I used is listen below:

Dim MyOutlook As Object
Dim MyNamespace As Object
Dim srcFolder As Object
Dim dstFolder As Object
Dim status As Long

Dim MyItem As Object
Dim folder As String

On Error GoTo EdifactFromOutlookFailed

' This could take a while
DoCmd.Hourglass True

' Open outlook
' Set MyOutlook = New Outlook.Application
Set MyOutlook = CreateObject("outlook.application")

' The the correct namespace
Set MyNamespace = MyOutlook.GetNamespace("MAPI")

' Get the inbox
Set srcFolder = MyNamespace.GetDefaultFolder(olFolderInbox)

' Get the correct folder for processed items
Set dstFolder = srcFolder.Folders(getEdifactMailFolder)

' Check for items with the correct subject : "CLOCKT;SRC"
For Each MyItem In srcFolder.items

' Must habe the correct subject
If MyItem.subject = "CLOCKT;SRC" And TypeName(MyItem) = "MailItem" Then

' Import this one
If (status = importAttachment(MyItem)) <> 0 Then

' Move this one into another folder
MyItem.Move dstFolder

Else

Err.Raise status, "EdifactImport"

End If

End If

Next MyItem

' This could take a while
DoCmd.Hourglass False

'Cleanup
Set MyNamespace = Nothing
Set MyOutlook = Nothing
Set srcFolder = Nothing
Set dstFolder = Nothing

' Done right here
Exit Sub
 

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

Top