从 Outlook 返回姓名和电子邮件地址列表到 vb.net 列表框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24417429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return list of names and email address from outlook to vb.net listbox
提问by Graham Jones
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.
我想从 Outlook 返回一个姓名和电子邮件地址列表,并将它们填充到列表框中,以便我可以选择我想要的。
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
采纳答案by Whitey
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:
您的第一个示例中的示例代码似乎是正确的,但“全局地址列表”访问字符串除外。尝试使用 1 作为数组索引:
Dim oGal As AddressList = oALs.Item(1)
I was able to access my contacts by doing this.
通过这样做,我能够访问我的联系人。
回答by doggy
Dim oGal As AddressList = oALs.Item(x) // <-- x represents each folder, global, contacts, public, ect
Dim oGal As AddressList = oALs.Item(x) // <-- x 代表每个文件夹,全局,联系人,公共等
回答by Marsh
This works for me:
这对我有用:
Dim oGal As Outlook.AddressList = oNS.GetGlobalAddressList

