【发布时间】:2013-07-26 19:49:17
【问题描述】:
我使用此代码从 Excel 发送电子邮件:
Sub Mail_workbook_Outlook_1()
'Working in Excel 2000-2013
'This example send the last saved version of the Activeworkbook
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.to = "ron@debruin.nl"
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.Body = "Hi there"
.Attachments.Add ActiveWorkbook.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
.Send ' <--------------------------------This is causing troubble
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
问题是.Send 未被识别为对象(或方法)。
其他命令正在运行(即显示、保存)。
我相信这个错误的存在是因为我工作的安全系统。我什至尝试过使用 CDO,但它无法正常工作。
【问题讨论】:
-
我创建了一个空白工作簿,将其保存为
C:\myDir\BlankBook.xlsx,并将上面的代码复制到代码模块中,仅将.to行更改为我的内部工作地址。代码对我来说没有问题。 -
将
.to行更改为我的gmail地址。代码再次运行良好。 -
这是一个奇怪的错误。您应该能够根据您的代码使用后期绑定,但让我们尝试早期绑定并查看错误是否仍然存在。您可以尝试启用对 MS Outlook 库的引用吗?然后
Dim OutApp as Outlook.Application和Dim OutMail as MailItem。 -
也许您可以发布您的原始代码?如果您使用的是上面的确切代码,我认为您不会看到任何错误,因为您的代码顶部附近有
On Error Resume Next? (其他的如果我错了请纠正我) -
什么版本的 Outlook?尝试
Debug.Print TypeName(OutMail)或在OutMail上设置监视:正在创建的对象类型是否正确?