Return list of names and email address from outlook to vb.net listbox

风流意气都作罢 提交于 2019-12-10 12:15:26

问题


I want to return from outlook, a list of names and email address and populate them in a listbox so that I can select the ones I want.

I'm looking to do this from the users local contact list and also the global address list on an exchange server.

I've seen many examples (Below) and nothing works, so any help would be most welcomed.

Thanks

Graham

I am using

Imports Microsoft.Office.Interop.Outlook
Imports Microsoft.Office.Interop

for both examples:

 Dim itemx As ListViewItem

        'Create an Outlook application.
        Dim oApp As Outlook._Application = New Outlook.Application()

        ' Get the MAPI namespace.
        Dim oNS As Outlook.NameSpace = oApp.Session
        ' Get the Global Address List.
        Dim oALs As Outlook.AddressLists = oNS.AddressLists
        Dim oGal As Outlook.AddressList = oALs.Item("Global Address List")

        ' Get all the entries.
        Dim oEntries As Outlook.AddressEntries = oGal.AddressEntries
        ' Get the first user.
        Dim oEntry As Outlook.AddressEntry = oEntries.GetFirst

        For i As Long = 1 To 10 ' Cut down to 100 as I dont want to load the full AB ** Need to Search rather than Loop **
            If oEntries(i).DisplayType = Outlook.OlDisplayType.olUser Then
                itemx = ListView1.Items.Add(oEntries(i).Name)
                itemx.SubItems.Add(oEntries(i).GetExchangeUser.JobTitle)
                itemx.SubItems.Add(oEntries(i).GetExchangeUser.BusinessTelephoneNumber)
                itemx.SubItems.Add(oEntries(i).GetExchangeUser.OfficeLocation)
                itemx.SubItems.Add(oEntries(i).GetExchangeUser.PrimarySmtpAddress)
                itemx.SubItems.Add(oEntries(i).GetExchangeUser.CompanyName)
                itemx.SubItems.Add(oEntries(i).GetExchangeUser.Alias)
            End If
        Next

        ' Clean up.
        oApp = Nothing
        oNS = Nothing
        oALs = Nothing
        oGal = Nothing
        oEntries = Nothing
        oEntry = Nothing

Also tried:

' Create Outlook application.
Dim oApp As Outlook.Application = New Outlook.Application()
'Get NameSpace and Logon.
Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
'oNS.Logon("Outlook", , False, True) ' TODO:
oNS.Logon("Outlook", Nothing, False, True)
' Get the first contact from the Contacts folder.
Dim cContacts As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
Dim oItems As Outlook.Items = cContacts.Items
Dim oCt As Outlook.ContactItem

Try
  oCt = oItems.GetFirst()
  ' Display some common properties.
  ListBox1.Items.Add(oCt.FullName)
  ListBox1.Items.Add(oCt.Title)
  ListBox1.Items.Add(oCt.Birthday)
  ListBox1.Items.Add(oCt.CompanyName)
  ListBox1.Items.Add(oCt.Department)
  ListBox1.Items.Add(oCt.Body)
  ListBox1.Items.Add(oCt.FileAs)
  ListBox1.Items.Add(oCt.Email1Address)
  ListBox1.Items.Add(oCt.BusinessHomePage)
  ListBox1.Items.Add(oCt.MailingAddress)
  ListBox1.Items.Add(oCt.BusinessAddress)
  ListBox1.Items.Add(oCt.OfficeLocation)
  ListBox1.Items.Add(oCt.Subject)
  ListBox1.Items.Add(oCt.JobTitle)
  Catch
      ListBox1.Items.Add("an error occurred")
     'Finally

     ' Display
     'oCt.Display(True)
     ' Log off.
     oNS.Logoff()
    ' Clean up.
     oApp = Nothing
     oNS = Nothing
     oItems = Nothing
     oCt = Nothing
  End Try

回答1:


It appears that the sample code in your first example is correct, except for the "Global Address List" access string. Try using 1 as the array index:

Dim oGal As AddressList = oALs.Item(1)

I was able to access my contacts by doing this.




回答2:


Dim oGal As AddressList = oALs.Item(x) // <-- x represents each folder, global, contacts, public, ect




回答3:


This works for me:

 Dim oGal As Outlook.AddressList = oNS.GetGlobalAddressList


来源:https://stackoverflow.com/questions/24417429/return-list-of-names-and-email-address-from-outlook-to-vb-net-listbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!