我正在通过VBA Outlook.Application Reference从Excel创建电子邮件。每封电子邮件都填充了我的Excel工作表中的数据,然后放入To / CC / BCC / Subject / Body字段。
现在,在Office 2010中运行此代码时,它可以顺利运行,但在Office 2013中包含To / CC / BCC /等的变量。显示时,数据不会显示在实际的电子邮件中。
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
答案 0 :(得分:0)
我不确定我是否可以直接帮助您,但我确实有一些我在网上找到的代码,我知道这个代码适用于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
答案 1 :(得分:0)
尝试引用Outlook库(工具 - >参考,然后选择Microsoft Outlook xx.x Object Library
),而不是创建Outlook对象。然后,您可以按如下方式引用它:
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
您可以在sub中添加此代码,也可以使用参数
从Sub调用此Sub