locked
Microsoft.Office.Interop.Outlook 12.0 RRS feed

  • Question

  • Hello, I have been able to connect to all versions Outlook except for Outlook 2007.  I have been using Visual Basic 2005. 

    This is the Code:

    Dim oOutlook As Outlook.Application = Nothing
    Dim oNameSpace As Outlook.NameSpace
    Dim oConFolder As Outlook.MAPIFolder = Nothing
    Dim oConItems As Outlook.Items = Nothing
    Dim oConItem As Outlook.ContactItem
    
    Try
    oOutlook = New Outlook.Application
    oNameSpace = oOutlook.GetNamespace("MAPI")
    oNameSpace.Logon("", "", False, True)
    oConFolder = oNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderConflicts)
    oConItems = oConFolder.Items
    Catch ex As Exception
    MessageBox.Show(ex.ToString)
    End Try
    
    For Each oConItem In oConItems
    messagebox.show(oConItem.FullName)
    Next
    

    The first contact displays in the messagebox, however, when the application gets  "Next" in the code, an error is thown ('that the call doesn't exist').  I have also tried to index and DirectCast oConItem.

    Dim app As Outlook.Application = New Outlook.ApplicationClass()
    Dim ns As Outlook.NameSpace = app.GetNamespace("MAPI")
    Dim contacts As Outlook.MAPIFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
    
    For i As Integer = 1 To contacts.Items.Count
    
    Dim contact As Outlook.ContactItem = DirectCast(contacts.Items(i), Outlook.ContactItem)
    messageBox.Show(contact.FullName)
    Application.DoEvents()
    Next

    The error occurs at 'contacts.Items(i).  It works for the first record, but when it gets to 2 it gives the same error as above.

    Any assistance is greatly appreciated.

    Thanks

    • Moved by Kee Poppy Sunday, January 16, 2011 9:41 AM (From:Visual Basic General)
    Saturday, January 15, 2011 6:24 AM

Answers

  • Is it possible that item 2 is actually not a contact item but perhaps a distribution list? That would account for your error. You need to test for the item type (Class or MessageClass) or do a TryCast() call and then check to see if your contact object is Nothing.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Trying Harder Than I Should Have To" <=?utf-8?B?VHJ5aW5nIEhhcmRlciBUaGFuIEkgU2hvdWxkIEhhdmUgVG8=?=> wrote in message news:b0dac064-dda7-4370-89d1-2cc055c46c7c...

    Hello, I have been able to connect to all versions Outlook except for Outlook 2007.  I have been using Visual Basic 2005. 

    This is the Code:

    Dim oOutlook As Outlook.Application = Nothing
    Dim oNameSpace As Outlook.NameSpace
    Dim oConFolder As Outlook.MAPIFolder = Nothing
    Dim oConItems As Outlook.Items = Nothing
    Dim oConItem As Outlook.ContactItem
    
    Try
    oOutlook = New Outlook.Application
    oNameSpace = oOutlook.GetNamespace("MAPI")
    oNameSpace.Logon("", "", False, True)
    oConFolder = oNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderConflicts)
    oConItems = oConFolder.Items
    Catch ex As Exception
    MessageBox.Show(ex.ToString)
    End Try
    
    For Each oConItem In oConItems
    messagebox.show(oConItem.FullName)
    Next
    

    The first contact displays in the messagebox, however, when the application gets  "Next" in the code, an error is thown ('that the call doesn't exist').  I have also tried to index and DirectCast oConItem.

    Dim app As Outlook.Application = New Outlook.ApplicationClass()
    Dim ns As Outlook.NameSpace = app.GetNamespace("MAPI")
    Dim contacts As Outlook.MAPIFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
    
    For i As Integer = 1 To contacts.Items.Count
    
    Dim contact As Outlook.ContactItem = DirectCast(contacts.Items(i), Outlook.ContactItem)
    messageBox.Show(contact.FullName)
    Application.DoEvents()
    Next

    The error occurs at 'contacts.Items(i).  It works for the first record, but when it gets to 2 it gives the same error as above.

    Any assistance is greatly appreciated.

    Thanks


    Ken Slovak MVP - Outlook
    • Marked as answer by 许阳(无锡) Thursday, January 20, 2011 4:34 AM
    Monday, January 17, 2011 3:11 PM

All replies

  • I can't help you with specific code as I haven't used Outlook in any apps.

    However see if this helps:

    http://msdn.microsoft.com/en-us/library/aa168022(v=office.11).aspx

    towards the bottom is a section on how to retrieve the contacts. I noticed some slight differences between yours and it.

    The other thing I noticed is your For...Next loop. Normally you would start at 0 and go to Count - 1 (most collections are zero based). So maybe try:

    For i As Integer = 0 To contacts.Items.Count - 1
    
    
    
    otherwise, have to wait for someone else or maybe search and/or post in the VSTO Forum

     http://social.msdn.microsoft.com/Forums/en-US/vsto/threads

    Saturday, January 15, 2011 7:10 AM
  • All Outlook collections start at a base of 1, not 0.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "jwavila" <=?utf-8?B?andhdmlsYQ==?=> wrote in message news:3bc537ab-e0e8-459e-8c7a-36c8136d3df1...

    I can't help you with specific code as I haven't used Outlook in any apps.

    However see if this helps:

    http://msdn.microsoft.com/en-us/library/aa168022(v=office.11).aspx

    towards the bottom is a section on how to retrieve the contacts. I noticed some slight differences between yours and it.

    The other thing I noticed is your For...Next loop. Normally you would start at 0 and go to Count - 1 (most collections are zero based). So maybe try:

    For i As Integer = 0 To contacts.Items.Count - 1
    
    
    
    otherwise, have to wait for someone else or maybe search and/or post in the VSTO Forum

     http://social.msdn.microsoft.com/Forums/en-US/vsto/threads


    Ken Slovak MVP - Outlook
    Monday, January 17, 2011 3:10 PM
  • Is it possible that item 2 is actually not a contact item but perhaps a distribution list? That would account for your error. You need to test for the item type (Class or MessageClass) or do a TryCast() call and then check to see if your contact object is Nothing.

    --
    Ken Slovak
    MVP - Outlook
    http://www.slovaktech.com
    Author: Professional Programming Outlook 2007
     
     
    "Trying Harder Than I Should Have To" <=?utf-8?B?VHJ5aW5nIEhhcmRlciBUaGFuIEkgU2hvdWxkIEhhdmUgVG8=?=> wrote in message news:b0dac064-dda7-4370-89d1-2cc055c46c7c...

    Hello, I have been able to connect to all versions Outlook except for Outlook 2007.  I have been using Visual Basic 2005. 

    This is the Code:

    Dim oOutlook As Outlook.Application = Nothing
    Dim oNameSpace As Outlook.NameSpace
    Dim oConFolder As Outlook.MAPIFolder = Nothing
    Dim oConItems As Outlook.Items = Nothing
    Dim oConItem As Outlook.ContactItem
    
    Try
    oOutlook = New Outlook.Application
    oNameSpace = oOutlook.GetNamespace("MAPI")
    oNameSpace.Logon("", "", False, True)
    oConFolder = oNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderConflicts)
    oConItems = oConFolder.Items
    Catch ex As Exception
    MessageBox.Show(ex.ToString)
    End Try
    
    For Each oConItem In oConItems
    messagebox.show(oConItem.FullName)
    Next
    

    The first contact displays in the messagebox, however, when the application gets  "Next" in the code, an error is thown ('that the call doesn't exist').  I have also tried to index and DirectCast oConItem.

    Dim app As Outlook.Application = New Outlook.ApplicationClass()
    Dim ns As Outlook.NameSpace = app.GetNamespace("MAPI")
    Dim contacts As Outlook.MAPIFolder = ns.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
    
    For i As Integer = 1 To contacts.Items.Count
    
    Dim contact As Outlook.ContactItem = DirectCast(contacts.Items(i), Outlook.ContactItem)
    messageBox.Show(contact.FullName)
    Application.DoEvents()
    Next

    The error occurs at 'contacts.Items(i).  It works for the first record, but when it gets to 2 it gives the same error as above.

    Any assistance is greatly appreciated.

    Thanks


    Ken Slovak MVP - Outlook
    • Marked as answer by 许阳(无锡) Thursday, January 20, 2011 4:34 AM
    Monday, January 17, 2011 3:11 PM
  • Thank you,

    If contacts.Items(i).Class = Outlook.OlObjectClass.olContact Then
                      
    Dim contact As Outlook.ContactItem = DirectCast(contacts.Items(i), Outlook.ContactItem)

    End if

    Worked Great!

    Thanks,

    Chuck

     

    Thursday, January 20, 2011 6:30 PM