Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何强制Outlook VBA使用特定的通讯簿?_Vba_Outlook - Fatal编程技术网

如何强制Outlook VBA使用特定的通讯簿?

如何强制Outlook VBA使用特定的通讯簿?,vba,outlook,Vba,Outlook,我编写了一个脚本,根据excel电子表格的内容将收到的电子邮件转发给合适的人 问题是这样做的方式是将收件人的姓名放入邮件的.To字段(即约翰·史密斯,而不是约翰·史密斯)。smith@example.com),然后在调用.Send方法时查找实际地址,Outlook似乎决定有时通过“LinkedIn社交连接器” 如何强制它在“全局地址列表”中查找此人的电子邮件地址?您可以从GAL获得地址,而不是由Outlook确定 从这里的例子 未测试代码 Option Explicit Sub DemoAE_

我编写了一个脚本,根据excel电子表格的内容将收到的电子邮件转发给合适的人

问题是这样做的方式是将收件人的姓名放入邮件的
.To
字段(即约翰·史密斯,而不是约翰·史密斯)。smith@example.com),然后在调用
.Send
方法时查找实际地址,Outlook似乎决定有时通过“LinkedIn社交连接器”


如何强制它在“全局地址列表”中查找此人的电子邮件地址?

您可以从GAL获得地址,而不是由Outlook确定

从这里的例子

未测试代码

Option Explicit

Sub DemoAE_ToName

    Dim colAL As Outlook.AddressLists 
    Dim oAL As Outlook.AddressList 
    Dim colAE As Outlook.AddressEntries 
    Dim oAE As Outlook.AddressEntry 
    Dim oExUser As Outlook.ExchangeUser 

    Set colAL = Application.Session.AddressLists 

    For Each oAL In colAL 

        'Address list is an Exchange Global Address List 
         If oAL.AddressListType = olExchangeGlobalAddressList Then

            Set colAE = oAL.AddressEntries 

            For Each oAE In colAE 

                ' no distribution lists
                If oAE.AddressEntryUserType = _
                    olExchangeUserAddressEntry _ 
                  Or oAE.AddressEntryUserType = _
                    olExchangeRemoteUserAddressEntry Then 

                    If oAE.Name = "John Smith" then
                        Set oExUser = oAE.GetExchangeUser 
                        Debug.Print (oExUser.PrimarySmtpAddress)
                    end if

                End If 

             Next 

         End If 

     Next 

End Sub
您可以设置一个函数来传递ToName并返回oExUser.PrimarySmtpAddress

而不是

For Each oAL In colAL
    If oAL.AddressListType = olExchangeGlobalAddressList Then
你应该能够删除一些代码

Set oAL = Application.Session.AddressLists("Global Address List")

编辑:回复:评论-艰难的人群

编辑2:显示限制不可用

已知名称时检索地址的测试代码

Sub AddressEntry_DirectAccess()

    Dim oNS As Namespace
    Dim oExUser As exchangeUser

    Set oNS = Application.GetNamespace("MAPI")
    Set oExUser = oNS.AddressLists("Global Address List").AddressEntries("Last, First").GetExchangeUser()

    If Not oExUser Is Nothing Then
        Debug.Print oExUser.name & ": " & oExUser.PrimarySmtpAddress
    End If

End Sub

首先,如果按Ctrl+Shift+B、Tools | Options,可以在Outlook中指定正确的名称解析顺序

如果您使用C++或Delphi,可以使用扩展MAPI:检索GAL的IABICALL接口并应用PROANANR限制。


如果使用是一个选项,则可以使用来检索GAL容器,然后使用来仅针对该特定容器进行解析。

这看起来可以解决问题,但我会遍历所有员工,是否有一个搜索功能可能更快?遍历GAL中的所有条目是一个可怕的主意。编辑2-替换使用工作代码进行编码。需要明确的是,使用
.AddressEntries(“Last,First”).GetExchangeUser()
的“测试代码”可以完美地工作,其余的不是最佳实践。