【问题标题】:Office 2013 - VBA Email does not display To/CC/BCC variablesOffice 2013 - VBA 电子邮件不显示收件人/抄送/密件抄送变量
【发布时间】:2016-09-22 09:35:16
【问题描述】:

我正在通过 VBA Outlook.Application Reference 从 Excel 创建电子邮件。每封电子邮件都填充了我的 Excel 表中的数据,然后放入 To/CC/BCC/Subject/Body 字段。

现在,当在 Office 2010 中运行此代码时,它可以顺利运行,但在 Office 2013 中,变量包含 To/CC/BCC/etc。显示时数据不会显示在实际电子邮件中。

此引用在 Office 2013 中是否发生了变化?

    Sub MailSheet()

    Dim OutApp As Object
    Dim outMail As Object
    Dim rng As Range

    ' set required variables
    Set Sourcewb = ActiveWorkbook
    Set Property = ActiveWorkbook.Sheets("Settings").Range("B4")
    Set Holidex = ActiveWorkbook.Sheets("Settings").Range("B5")
    Set SendTo = ActiveWorkbook.Sheets("Settings").Range("B29")
    Set SendCC = ActiveWorkbook.Sheets("Settings").Range("B30")
    Set rng = Sheets("Mail").Range("A1:F80")

    ' set email variables
    Set OutApp = CreateObject("Outlook.Application")
    Set outMail = OutApp.CreateItem(0)

' some code

        ' get ready to mail
        With outMail
            .To = SendTo
            .ReplyRecipients.Add ""
            .CC = SendCC
            .BCC = ""
            .Subject = Holidex & " - Daily Email"
            .HTMLBody = RangetoHTML(rng)

            ' display email before sending
            .Display   '.Send or use .Display
        End With

' some code

    ' Clean up
    Set outMail = Nothing
    Set OutApp = Nothing

end Sub

【问题讨论】:

  • 首先,不要在回复收件人中添加空字符串。其次,如果您使用 Recipients.Add 而不是设置 To/CC/BCC 属性,您会使用不同的结果吗?

标签: excel vba email outlook


【解决方案1】:

与其创建 Outlook 对象,不如尝试引用 Outlook 库(工具 -> 引用,然后选择 Microsoft Outlook xx.x Object Library)。然后您可以按如下方式引用它:

Sub SendAnEmail()

    Dim oOlApp As Outlook.Application: Set oOlApp = Outlook.Application
    Dim oMailItem As Outlook.MailItem: Set oMailItem = oOlApp.CreateItem(olMailItem)

    oMailItem.To = "myemail@test.com"
    oMailItem.CC = ""
    oMailItem.BCC = "myemail@test.com"
    oMailItem.Subject = Sheet1.Cells(15, "D")
    oMailItem.HTMLBody = "Again .. testing"
    oMailItem.Display

    Set oMailItem = Nothing
    Set oOlApp = Nothing

End Sub

您可以在您的子程序中添加此代码,也可以使用参数从您的子程序调用此子程序

【讨论】:

  • 我知道您来自哪里,但我可能必须将其部署在用户无法激活引用的机器上。目前,我正在执行此代码之前检查 Outlook 是否已打开并运行。有什么办法可以手动激活引用?
  • 我刚刚尝试了与您类似的代码,它在 Office Professional Plus 2013 中对我有用。我在 Class Module 中有代码,这样我在初始化课程时检查 Outlook 是否打开.你有哪个版本的office 2013?
【解决方案2】:

不确定我是否可以直接帮助您,但我确实有一些我在网上找到的代码,我知道这些代码适用于 Outlook 2016,如果有帮助,将在此处分享:

Sub OutlookMail_1()
'Automate Sending Emails from Excel, using Outlook.
'Send text and also contents from the host workbook's worksheet range
' as Mail Body, and add an attachment with the mail.
'Automating using Early Binding: Add a reference to the Outlook Object Library
' in Excel (your host application) by clicking Tools-References in VBE,
' which will enable using Outlook's predefined constants.
'Once this reference is added, a new instance of
' Outlook application can be created by using the New keyword.

'variables declared as a specific object type
'  ie. specific to the application which is being automated:
Dim applOL As Outlook.Application
Dim miOL As Outlook.MailItem
Dim recptOL As Outlook.Recipient
Dim ws As Worksheet
Dim name As String
Dim email As String
Dim nominees As Range
Dim number As String

'set worksheet:
Set ws = ThisWorkbook.Sheets("Sheet1")

'Create a new instance of the Outlook application.
' Set the Application object as follows:
Set applOL = New Outlook.Application

'create mail item:
Set miOL = applOL.CreateItem(olMailItem)

'Add mail recipients, either the email id or their name in your address book.
' Invalid ids will result in code error.
Set recptOL = miOL.Recipients.Add("Main recipient email")
recptOL.Type = olTo

Set recptOL = miOL.Recipients.Add("BCC Email")
recptOL.Type = olbcc

Set recptOL = miOL.Recipients.Add("BCC Email")
recptOL.Type = olbcc

Set recptOL = miOL.Recipients.Add("BCC Email")
recptOL.Type = olbcc

'with the mail item:
With miOL

'subject of the mail:
.Subject = "Subject"    

'Chr(10) represents line feed/new line, & Chr(13) represents carriage return.

' Send text and also contents from
'  the host workbook's worksheet range as Mail Body.
.Body = "BODY OF EMAIL"

'set importance level for the mail:
.Importance = olImportanceHigh
'add an attachment to the mail:

'send the mail:

.Display

End With  

'clear the object variables:
Set applOL = Nothing
Set miOL = Nothing
Set recptOL = Nothing 

End Sub

我设置的一些变量是多余的,因为我稍微编辑了代码以保护隐私,但如果有帮助,请告诉我!

如果您想使用 CC 而不是 Bcc,那么只需将代码更改为:

recptOL.Type = olcc

【讨论】:

  • 我还没有使用过 Office 2016,但我会记住的,谢谢。
猜你喜欢
  • 2011-07-24
  • 1970-01-01
  • 2013-12-22
  • 2015-12-19
  • 1970-01-01
  • 2019-01-01
  • 1970-01-01
  • 2013-06-08
  • 2012-03-20
相关资源
最近更新 更多