简体   繁体   English

如何使用 VBA GetSelectNameDialog 从全局地址列表中确定 CC / BCC 选择

[英]How to determine CC / BCC selection using VBA GetSelectNameDialog from Global Address List

Hi knowledgeable people!嗨,知识渊博的人!

Background:背景:

I am developing a custom mail merge tool in MS Word VBA for our team so that we have extra functionality beyond the standard Office Word mail merge package.我正在为我们的团队在 MS Word VBA 中开发一个自定义邮件合并工具,以便我们拥有超出标准 Office Word 邮件合并 package 的额外功能。 3rd party products or add-ons are not possible.第三方产品或附加组件是不可能的。 But, the ability to automatically attach specific files, custom subject line etc, would save us a lot of time and effort.但是,自动附加特定文件、自定义主题行等的能力将为我们节省大量时间和精力。

One of the features is to enable the user to select additional carbon-copy (CC) or blind carbon-copy (BCC) email accounts to append to the mail merge from our corporate Microsoft Exchange Global Address List (GAL).其中一项功能是使用户能够将 select 附加抄送 (CC) 或盲抄送 (BCC) email 帐户从我们的公司 Microsoft Exchange 全球地址列表合并到 Z9516DFB15F51C7EE19A4D46B8C0DBE1DGAL 到邮件。 The user may need to select multiple CC or BCC email accounts.用户可能需要 select 多个 CC 或 BCC email 帐户。

Problem:问题:

Using a previous question & answer ( 30918152 ) I was able to call the address book GAL and customise the To: / CC: / BCC: labels.使用之前的问答 ( 30918152 ),我可以调用地址簿 GAL 并自定义To: / CC: / BCC:标签。 The code is able to retrieve the selected exchange accounts in the .Recipients collection, but I am struggling to determine which selections are CC or BCC.该代码能够检索.Recipients集合中选定的交换帐户,但我正在努力确定哪些选择是 CC 或 BCC。

I am aware that Outlook.Recipient.Type returns a variable type Long , which relates to From: / To: / CC: / BCC: But, when I debug.print 'recipient.type' always returns 1 even when CC or BCC is selected.我知道Outlook.Recipient.Type返回一个变量类型Long ,它与 From: / To: / CC: / BCC: 但是,当我debug.print 'recipient.type'总是返回 1 即使 CC 或 BCC 是选择。

Does anyone know where I am going wrong?有谁知道我哪里出错了?

Progress so far:目前进展:

I have searched MSDN, run multiple web searches and scoured places like Stack Overflow, VBOffice.net, but haven't what I'm looking for.我搜索了 MSDN,运行了多个 web 搜索并搜索了 Stack Overflow、VBOffice.net 等地方,但没有我想要的。 I am self-taught, so suspect my fundamental problem is lack of understanding of the MSDN page on SelectNamesDialog.Recipients我是自学成才的,所以怀疑我的根本问题是对SelectNamesDialog.Recipients上的 MSDN 页面缺乏了解

Code:代码:

Private Sub cmdSetProjectMember1_Click()
' CODE TO SELECT FROM ADDRESS BOOK AND TAKE EMAIL ADDRESS WHEN IT IS AN EXCHANGE USER.
' TAKEN FROM https://stackoverflow.com/questions/30918152/opening-outlook-address-book-from-excel

  Dim olApp As Outlook.Application
  Dim oDialog As SelectNamesDialog
  Dim oGAL As AddressList
  Dim myAddrEntry As AddressEntry
  Dim exchUser As Outlook.ExchangeUser
  Dim TEST_Recipient As Outlook.Recipient

  Dim AliasName As String
  Dim FirstName As String
  Dim LastName As String
  Dim EmailAddress As String

    Set aOutlook = GetObject(, "Outlook.Application")
    Set oDialog = aOutlook.Session.GetSelectNamesDialog
    Set oGAL = aOutlook.GetNamespace("MAPI").AddressLists("Global Address List")

    With oDialog
        .AllowMultipleSelection = True
        .InitialAddressList = oGAL
        .ShowOnlyInitialAddressList = True
        .Caption = "Custom mail merge tool  *****  | |  *****  SELECT EMAIL FROM ADDRESS BOOK"
        .NumberOfRecipientSelectors = olShowToCc
        .ToLabel = "Select CC:"
        .CcLabel = "Select BCC:"

        If .Display Then
                AliasName = oDialog.Recipients.Item(1).Name
                Set myAddrEntry = oGAL.AddressEntries(AliasName)
                Set exchUser = myAddrEntry.GetExchangeUser

                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

                Set TEST_Recipient = oDialog.Recipients.Item(1)
                 Debug.Print TEST_Recipient.Type

                 If TEST_Recipient.Type = olCC Then
                    MsgBox "Carbon Copy"

                Else
                    MsgBox "NOT CC"

                End If

            End If

        End If

    End With

 Set olApp = Nothing
 Set oDialog = Nothing
 Set oGAL = Nothing
 Set myAddrEntry = Nothing
 Set exchUser = Nothing

End Sub

Thank you to @Eugene for help pointing me to LOGON感谢@Eugene帮助将我指向LOGON

For some reason, because Outlook is already running, the instance of the address book couldn't pull through details when called again separately through MS Word VBA.由于某种原因,由于 Outlook 已经在运行,通过 MS Word VBA 再次单独调用时,地址簿实例无法提取详细信息。

Here is my final code to make this work, complete with a loop for capturing details for multiple CC / BCC selections.这是我完成这项工作的最终代码,其中包含一个用于捕获多个 CC / BCC 选择的详细信息的循环。

Private Sub cmdSetProjectMember1_Click()
' CODE TO SELECT FROM ADDRESS BOOK AND TAKE EMAIL ADDRESS WHEN IT IS AN EXCHANGE USER.
' TAKEN FROM https://stackoverflow.com/questions/30918152/opening-outlook-address-book-from-excel

  Dim olApp As Outlook.Application
  Dim oNS As Outlook.Namespace
  Dim oDialog As SelectNamesDialog
  Dim oGAL As AddressList
  Dim myAddrEntry As AddressEntry
  Dim exchUser As Outlook.ExchangeUser
  Dim TEST_Recipient As Outlook.Recipient

  Dim AliasName As String
  Dim FirstName As String
  Dim LastName As String
  Dim EmailAddress As String

  ' New dimension variables to capture multiple address book selections
  Dim iRecipientCount As Integer
  Dim iLoop As Integer

    Set aOutlook = GetObject(, "Outlook.Application")

    ' New code for LOGON inserted here
    Set oNS = aOutlook.GetNamespace("MAPI")
    oNS.Logon "LatestProfile", , True, True


    Set oDialog = aOutlook.Session.GetSelectNamesDialog
    Set oGAL = aOutlook.GetNamespace("MAPI").AddressLists("Global Address List")

    With oDialog
        .AllowMultipleSelection = True
        .InitialAddressList = oGAL
        .ShowOnlyInitialAddressList = True
        .Caption = "Custom mail merge tool  *****  | |  *****  SELECT EMAIL FROM ADDRESS BOOK"
        .NumberOfRecipientSelectors = olShowToCcBcc
        .ToLabel = "Select FROM:"
        .CcLabel = "Select CC:"
        .BccLabel = "Select BCC:"

        If .Display Then
          AliasName = oDialog.Recipients.Item(1).Name
          Set myAddrEntry = oGAL.AddressEntries(AliasName)
          Set exchUser = myAddrEntry.GetExchangeUser

          If Not exchUser Is Nothing Then
            iRecipientCount = oDialog.Recipients.Count

            For iLoop = 1 To iRecipientCount
              Set TEST_Recipient = oDialog.Recipients.Item(iLoop)

              Debug.Print TEST_Recipient.Index
              Debug.Print TEST_Recipient.Type
              Debug.Print "NEXT"

              Select Case TEST_Recipient.Type
                Case 1
                  MsgBox TEST_Recipient.Name & vbNewLine & "Selected FROM:"

                Case 2
                  MsgBox TEST_Recipient.Name & vbNewLine & "Selected CC:"

                Case 3
                  MsgBox TEST_Recipient.Name & vbNewLine & "Selected BCC:"

               End Select

            Next iLoop

          End If

        End If

    End With

 Set olApp = Nothing
 Set oDialog = Nothing
 Set oGAL = Nothing
 Set myAddrEntry = Nothing
 Set exchUser = Nothing

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用VBA在Outlook中自动填充“收件人”,“抄送”,“密件抄送”字段 - Auto-populating To,CC,BCC fields in Outlook using VBA 如果关联名称包含字符串,如何从 Outlook VBA 的全局地址列表中提取 Email 地址 - How to Pull Email Address from Global Address List in Outlook VBA if the Associated Name contains String Outlook 2007 VBA - BCC 取决于 To/CC 收件人 - Outlook 2007 VBA - BCC depending on To/CC Recipients 从 Access VBA 查询 Outlook 全局地址列表 - Query Outlook Global Address List From Access VBA Outlook 2013 VBA 从全局地址列表中获取所需数据 - Outlook 2013 VBA Getting required data from Global Address List 如何在向唯一人发送一组行的同时在 VBA 宏代码中嵌入 CC 和 BCC - How to Embedd CC and BCC in the VBA Macro Code while send set of rows to unique person Office 2013-VBA电子邮件不显示To / CC / BCC变量 - Office 2013 - VBA Email does not display To/CC/BCC variables 使用VBA创建一个规则,以在传出的Outlook电子邮件中添加密件抄送地址,具体取决于所使用的帐户 - Using VBA create a rule to add a BCC address on outgoing Outlook email, depending on the account used 用于验证 Outlook 全局地址列表中的电子邮件地址的 VBA 代码 - VBA CODE to Verify Email Address Found in Outlook Global Address List 如何使用VBA从excel列表框中的多项选择中获得结果? - How to get results based on multiple selection from the list box in excel using VBA?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM