如何从Outlook通讯簿中获取基于Excel中的值(VBA)

我有以下代码的作品(我发现它在论坛上):

Public Sub GetUsers() Dim myolApp As Outlook.Application Dim myNameSpace As Namespace Dim myAddrList As AddressList Dim myAddrEntries As addressEntry Dim AliasName As String Dim i As Integer, r As Integer Dim EndRow As Integer, n As Integer Dim myStr As String, c As Range Dim myPhone As String 'Dim propertyAccessor As Outlook.propertyAccessor 'This only works with 2007 and may help you out Set myolApp = CreateObject("Outlook.Application") Set myNameSpace = myolApp.GetNamespace("MAPI") Set myAddrList = myNameSpace.addressLists("Global Address List") Dim FullName As String, LastName As String, FirstName As String Dim StartRow As Integer EndRow = Cells(Rows.Count, 3).End(xlUp).Row StartRow = InputBox("At which row should this start?", "Start Row", 4) For Each c In Range("A" & StartRow & ":A" & CStr(EndRow)) AliasName = LCase(Trim(c)) c = AliasName Set myAddrEntries = myAddrList.addressEntries(AliasName) FullName = myAddrEntries.Name FirstName = Trim(Mid(FullName, InStr(FullName, "(") + 1, _ InStrRev(FullName, " ") - InStr(FullName, "("))) LastName = Right(FullName, Len(FullName) - InStrRev(FullName, " ")) LastName = Left(LastName, Len(LastName) - 1) c.Offset(0, 1) = FirstName c.Offset(0, 2) = LastName c.Offset(0, 3) = FirstName & " " & LastName Next c End Sub 

当我提供一个名字(第一个或最后一个)时,它会在地址簿中查找它,并返回find的人的名字和姓氏。

我想提供这个人的企业ID,find它,然后返回其他信息(位置,电话号码等)。

我无法弄清楚如何做到这一点。 首先,我不知道如何才能只search别名,只要我可以告诉它只是局部variables声明。 另外,当我尝试提取其他信息时,例如:

 HomeState = myAddrEntries.HomeState 

我得到一个错误:对象不支持这个属性或方法。 我不知道这个属性会被称为什么 – 我在网上找不到任何文件显示属性如何命名(甚至当我searchMAPI docuemntation)。

所以,我的问题是 – 我怎样才能使用这个代码来searchID和返回其他属性,如位置,数量等等。 – 我怎么能概括该进程 – 是否有这些字段名称被调用的列表,是一个生成列表的方法?

谢谢!

让我们看看这是否能帮助你。 我不是Outlook VBA的专家,但它大部分是相同的,只是find文档的问题。

collections此页:

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

具体来说,你可以看看AddressEntry对象的条目:

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

从那里你可以看到可用的属性/方法的列表。 我相信应该回答你的第二个问题, 我得到一个错误:对象不支持这个属性或方法。 我不知道这个财产会被称为什么

Homestate不是AddressEntry对象的属性。

当我提供一个名字(第一个或最后一个)时,它会在地址簿中查找它,并返回find的人的名字和姓氏。

不要期望这是100%可靠的

我用6个名字testing了它,其中4个是正确的。 3名是罕见的姓氏。 一个是全名,令人惊讶地返回错误的结果。 你的旅费可能会改变。

这不适用于任何大型组织。 如果你有一个小的地址列表,那么基于一个简单的名字/姓氏string就可以很容易的唯一parsing。 但是否则,这是不可靠的。

你有几个问题:

我想提供这个人的企业ID,find它,然后返回其他信息(位置,电话号码等)。

我不认为这是Outlook如何parsing别名的电子邮件地址。 您将需要引用一些外部数据库来执行这样的查询。

我不知道如何才能知道只search别名,据我所知,这只是局部variables声明。

在示例代码中, AliasName是一个局部variables,但是从用户input(例如Excel电子表格中的单元格)分配了一个值。 所以macros观正在读取一些价值观,并试图通过地址簿解决它们。

正如我上面提到的,这只是一个简单的string将唯一解决正确的个人的可能性。

另外,当我尝试提取其他信息时,例如:

 HomeState = myAddrEntries.HomeState 

我得到一个错误:对象不支持这个属性或方法。 我不知道这个属性会被称为什么 – 我在网上找不到任何文件显示属性如何命名(甚至当我searchMAPI docuemntation)。

可以有更好的解决scheme?

是。 是的,可以。

如果在对象模型中进行挖掘,会发现两个看起来很有前途的项目, GetContact方法返回一个ContactItem (不幸的是,这不是我们想要的), GetExchangeUser返回一个ExchangeUser 。 我认为这是最接近你想要的,因为它包含你正在寻找的大部分信息。

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

我修改你的代码如下:

 Option Explicit Public Sub GetUsers() Dim myolApp As Outlook.Application Dim myNameSpace As Namespace Dim myAddrList As AddressList Dim myAddrEntry As addressEntry 'I changed this variable to avoid ambiguity Dim AliasName As String Dim i As Integer, r As Integer Dim c As Range Dim EndRow As Integer, n As Integer Dim exchUser As Outlook.ExchangeUser Set myolApp = CreateObject("Outlook.Application") Set myNameSpace = myolApp.GetNamespace("MAPI") Set myAddrList = myNameSpace.addressLists("Global Address List") Dim FullName As String, LastName As String, FirstName As String Dim HomeState As String, PhoneNum As String Dim StartRow As Integer EndRow = Cells(Rows.Count, 3).End(xlUp).Row StartRow = InputBox("At which row should this start?", "Start Row", 4) For Each c In Range("A" & StartRow & ":A" & CStr(EndRow)) AliasName = LCase(Trim(c)) c = AliasName Set myAddrEntry = myAddrList.addressEntries(AliasName) Set exchUser = myAddrEntry.GetExchangeUser If Not exchUser Is Nothing Then FirstName = exchUser.FirstName LastName = exchUser.LastName HomeState = exchUser.StateOrProvince PhoneNum = exchUser.BusinessTelephoneNumber 'etc... End If Next c End Sub