[英]How to determine CC / BCC selection using VBA GetSelectNameDialog from Global Address List
Hi knowledgeable people!嗨,知识渊博的人!
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 帐户。
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?有谁知道我哪里出错了?
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 页面缺乏了解
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.