【问题标题】:Control contents of email address fields在 MS Word 2016 中控制电子邮件字段的内容
【发布时间】:2020-09-21 09:01:59
【问题描述】:

我想将 word 文档的正文作为电子邮件发送,但我希望用户能够使用通讯簿从 MS Word 2016 中选择要将其发送给哪些收件人。当用户选择要发送的电子邮件时到,我希望他们只能输入密件抄送字段。

我卡住的地方是监视 to/from/CC/BCC 字段的更改,然后将这些更改移动到 BCC。该文档似乎表明了 Inspectors 的使用,但没有具体说明访问这些字段的内容。

对此我有两种方法,一种是打开一个新的 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 = "recipient@mail.com"
    'Set the recipient for a copy
    .CC = "recipient2@mail.com"
    '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

【问题讨论】:

    标签: vba outlook


    【解决方案1】:

    您似乎对SelectNamesDialog 对象感兴趣,该对象显示“选择名称”对话框供用户从一个或多个地址列表中选择条目,并返回由属性SelectNamesDialog.Recipients 指定的集合对象中的选定条目.

    SelectNamesDialog.Display 显示的对话框类似于 Outlook 用户界面中的“选择名称”对话框。它观察内置选择名称对话框的大小和位置设置。但是,其默认状态不会在ToCcBcc 编辑框上方显示邮件收件人。

    以下代码示例显示了如何创建邮件项,允许用户在“选择名称”对话框中从 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
    

    【讨论】:

    • 这是我放弃的一个旧项目,但这是我正在寻找的解决方案。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多