将名单和电子邮件地址从outlook返回到vb.net listbox [英] Return list of names and email address from outlook to vb.net listbox

查看:132
本文介绍了将名单和电子邮件地址从outlook返回到vb.net listbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从outlook返回名单和电子邮件地址列表,并将其填充到列表框中,以便我可以选择我想要的。

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.

谢谢

Graham

我正在使用

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

两个例子:

 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

还试过:

' 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.

这篇关于将名单和电子邮件地址从outlook返回到vb.net listbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆