excel - 检索全局地址列表选择的电子邮件地址
问题描述
我想在 Excel VBA 中打开全局地址列表并选择收件人并使用电子邮件地址填充 Excel 用户表单。
它填充 GAL 中的下一个条目,而不是我选择的那个。我从这里改编了一些代码。进一步看,它将显示正确的用户,但前提是一个人有该姓氏。
Private Sub cmdSetProjectMember1_Click()
Dim olApp As Outlook.Application
Dim oDialog As SelectNamesDialog
Dim oGAL As AddressList
Dim myAddrEntry As Outlook.AddressEntry
Dim exchUser As Outlook.ExchangeUser
Dim sAliasName As String
Dim FirstName As String
Dim LastName As String
Dim EmailAddress As String
Set olApp = Outlook.Application
Set olns = olApp.GetNamespace("MAPI")
Set oGAL = olApp.GetNamespace("MAPI").AddressLists("Global Address List")
Set oDialog = olApp.Session.GetSelectNamesDialog
With oDialog
.AllowMultipleSelection = False
.InitialAddressList = oGAL
.ShowOnlyInitialAddressList = True
If .Display Then
sAliasName = oDialog.Recipients.Item(1).Name
Set myAddrEntry = oGAL.AddressEntries(sAliasName)
Set exchUser = myAddrEntry.GetExchangeUser
End If
End With
If Not exchUser Is Nothing Then
FirstName = exchUser.FirstName
LastName = exchUser.LastName
EmailAddress = exchUser.PrimarySmtpAddress
'...
MsgBox "You selected contact: " & vbNewLine & _
"FirstName: " & FirstName & vbNewLine & _
"LastName:" & LastName & vbNewLine & _
"EmailAddress: " & EmailAddress
End If
UserForm1.TextBox5.Value = EmailAddress
Set olApp = Nothing
Set oDialog = Nothing
Set oGAL = Nothing
Set myAddrEntry = Nothing
Set exchUser = Nothing
End Sub
解决方案
我不确定您为什么使用以下行:
Set myAddrEntry = oGAL.AddressEntries(sAliasName)
将您的代码更改为
If .Display Then
Set myAddrEntry = oDialog.Recipients.Item(1).AddressEntry
Set exchUser = myAddrEntry.GetExchangeUser
End If
推荐阅读
- c++ - 使用迭代器对向量进行 C++ 递归初始化会产生不一致的结果
- javascript - Firebase Web:方法不会在 child_added 内第一次触发
- ios - 如何将 coredata 中的条目导出到 csv 文件中以通过电子邮件发送?
- java - 捕获并增强 LockedException 和 DisabledException
- java - 模块化 Spring Security SecurityWebFilterChain
- rest - 在共享上下文中通过 API 运行 Databricks 笔记本作业
- javascript - 如何将我的在线 pine 脚本转换为图表库的指标?
- r - 查找一个值是否大于向量中所有先前值的函数
- postgresql - Postgresql 等效于 SQL Server CONTEXT_INFO
- laravel-5 - 迁移中的数组到字符串转换错误