我想问一下是否有一种方法可以从Outlook通讯录中检索用户表单文本框中输入的值并检索它。
例如,我的textbox1让用户输入要搜索的人的全名,并带有搜索按钮,textbox2将基于textbox1从Outlook通讯录中检索所有电子邮件地址。
当前我所拥有的是一个模块调用,用于检索电子邮件
Option Explicit
Sub GetAddresses()
Dim o, AddressList, AddressEntry
Dim c As Range, r As Range, AddressName As String
Set o = CreateObject("Outlook.Application")
Set AddressList = o.Session.AddressLists("Contacts")
'Chage this range to include the first names only. AddressName assignment line handles concatenating the last name.
Set r = Add.Emailname
For Each c In r
AddressName = c.Value & " " & c.Offset(0, 1).Value
For Each AddressEntry In AddressList.AddressEntries
If AddressEntry.name = AddressName Then
c.Offset(0, 2).Value = AddressEntry.Address
Exit For
End If
Next AddressEntry
Next c
End Sub
在我的用户表单中,搜索按钮
Private Sub Searchbutton_Click()
Call GetAddresses
End Sub
代码是我从网上看到的。有人可以帮我编辑和指导我吗?
答案 0 :(得分:1)
我看到您有copied您的代码。该代码旨在在一定范围内循环。您可以简单地删除循环并添加文本框值,然后将其分配给searchbutton。但是,您需要一个.ListBox1
(而不是.TextBox2
),因为您希望所有的匹配都显示在列表中。我还实现了.Instr
函数,以查看searchvalue是否出现在联系人名称中(请确保使用LCase
)。这将使搜索姓氏更加容易。代码如下:
Option Explicit
Private Sub GetAddresses()
Dim o, AddressList, AddressEntry
Dim AddressName As String
Set o = CreateObject("Outlook.Application")
Set AddressList = o.Session.AddressLists("Contacts")
'Change this range accordingly
AddressName = UserForm1.TextBox1.Value
For Each AddressEntry In AddressList.AddressEntries
If InStr(1, LCase(AddressEntry.Name), LCase(AddressName)) <> 0 Then
UserForm1.ListBox1.AddItem AddressEntry.Address
End If
Next AddressEntry
End Sub
Private Sub Searchbutton_Click()
UserForm1.ListBox1.Clear
Call GetAddresses
End Sub