【问题标题】:retrieving email address from outlook through userform通过用户表单从 Outlook 中检索电子邮件地址
【发布时间】:2020-06-24 04:06:13
【问题描述】:

我想问是否有办法从用户表单文本框中输入的值中从 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

代码是我从网上看到的。谁能帮我编辑和指导我?

【问题讨论】:

  • 不清楚您要做什么。 the values enter in user form textbox?如果你展示你所拥有的和你正在尝试做的事情的例子,也许会有所帮助。
  • 嗨@ashleedawg,例如在userform textbox1中,用户输入姓名,Tom,James,然后当我点击搜索按钮时,textbox2将具有来自地址框中的电子邮件地址的结果展望未来。
  • 我认为您在使用 Sue Mosher 的 ResolveDisplayNameToSMTP 功能。我找不到原始网站,但这是该网站上的链接:Creating a “Check Names” button in Excel

标签: excel vba outlook


【解决方案1】:

我看到你有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

【讨论】:

  • 嗨 @jvdv ,如果我在 textbox1 中有多个名称。我应该如何更改代码?
  • 为什么一个文本框中会有多个名字?考虑:A)制作更多文本框 B)从一个文本框搜索多次(如果您希望在新搜索中将结果添加到列表框,可能不要清除您的列表框)
  • 我认为解决方案 B 有效。你能指导我吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-12
  • 2020-10-02
  • 2016-02-29
  • 1970-01-01
  • 2020-09-28
  • 2013-08-20
  • 2013-09-07
相关资源
最近更新 更多