我想询问是否有办法从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
代码是我在网上看到的。任何人都可以帮我编辑和指导我吗?
我看到你有copied你的代码。此代码旨在循环一个范围。您可以简单地删除循环并实现文本框值并将其分配给搜索按钮。但是你需要一个.ListBox1
(而不是.TextBox2
),因为你想让所有联系人点击出现在列表中。我还实现了.Instr
函数,以查看搜索值是否出现在联系人名称中(使用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