我想将word文档的正文作为电子邮件发送,但我希望用户能够使用地址簿从MS Word 2016中选择要发送给哪些收件人。当用户选择要发送的邮件时,我希望他们只能够在BCC字段中放入。
我被卡住的地方是监控tofromCCBCC字段的变化,然后将这些变化移动到BCC。文档似乎指出了检查器的用法,但没有具体说明如何访问这些字段的内容。
我有两种方法,一种是打开一个新的Outlook邮件项,将word文件的内容加载到其中,然后尝试用这种方式监控字段。
另一种方法是直接从word中使用快速访问工具栏选项 "发送到邮件收件人 "打开邮件信息,然后用这种方式发送。但我不知道根据我阅读的内容,这是否真的是一个选项,那些字段是否可以通过VBA访问。
下面是我目前的代码示例。
Dim bStarted As Boolean
Dim oOutlookApp As Outlook.Application
Dim oItem As Outlook.MailItem
On Error Resume Next
'Get Outlook if it's running
Set oOutlookApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
'Outlook wasn't running, start it from code
Set oOutlookApp = CreateObject("Outlook.Application")
bStarted = True
End If
'Create a new mailitem
Set oItem = oOutlookApp.CreateItem(olMailItem)
With oItem
'Set the recipient for the new email
.To = "[email protected]"
'Set the recipient for a copy
.CC = "[email protected]"
'Set the subject
.Subject = "New subject"
'The content of the document is used as the body for the email
.Body = ActiveDocument.Content
.Send
End With
If bStarted Then
'If we started Outlook from code, then close it
oOutlookApp.Quit
End If
'Clean up
Set oItem = Nothing
Set oOutlookApp = Nothing
End Sub
看来你对 选择名称对话框 对象,该对象显示 "选择名称 "对话框,供用户从一个或多个地址列表中选择条目,并将所选条目返回到由属性 SelectNamesDialog.Recipients
.
显示的对话框是由 SelectNamesDialog.Display
类似于Outlook用户界面中的 "选择名称 "对话框。它遵守内置的 "选择名称 "对话框的大小和位置设置。然而,它的默认状态是不显示邮件收件人在上面的 To
, Cc
和 Bcc
编辑框。
下面的代码示例展示了如何创建一个邮件项,让用户在 "选择名称 "对话框中从Exchange全局地址列表中选择收件人,如果用户选择的收件人可以完全解析,则发送邮件项。
Sub SelectRecipients()
Dim oMsg As MailItem
Set oMsg = Application.CreateItem(olMailItem)
Dim oDialog As SelectNamesDialog
Set oDialog = Application.Session.GetSelectNamesDialog
With oDialog
.InitialAddressList = _
Application.Session.GetGlobalAddressList
.Recipients = oMsg.Recipients
If .Display Then
'Recipients Resolved
oMsg.Subject = "Hello"
oMsg.Send
End If
End With
End Sub