在 Send 方法失​​败的情况下使用 Outlook 发送邮件

Posted

技术标签:

【中文标题】在 Send 方法失​​败的情况下使用 Outlook 发送邮件【英文标题】:Sending mail using Outlook where the Send method fails 【发布时间】: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.ApplicationDim OutMail as MailItem 也许您可以发布您的原始代码?如果您使用的是上面的确切代码,我认为您不会看到任何错误,因为您的代码顶部附近有 On Error Resume Next? (其他的如果我错了请纠正我) 什么版本的 Outlook?尝试Debug.Print TypeName(OutMail) 或在OutMail 上设置监视:正在创建的对象类型是否正确? 【参考方案1】:

.Send 更改为.Display 并将SendKeys "^ENTER" 放在With OutMail 行之前。

【讨论】:

谢谢!一个很好的解决方法:) 我编辑了您的答案以反映解决我的问题的方法。希望你没问题?:) 别担心,很高兴你最终解决了我的朋友 :-)【参考方案2】:

试试这个代码。

Sub Email_ActiveSheet_As_PDF()

'Do not forget to change the email ID
'before running this code

Dim OutApp As Object
Dim OutMail As Object
Dim TempFilePath As String
Dim TempFileName As String
Dim FileFullPath As String

With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With

' Temporary file path where pdf
' file will be saved before
' sending it in email by attaching it.

TempFilePath = Environ$("temp") & "\"

' Now append a date and time stamp
' in your pdf file name. Naming convention
' can be changed based on your requirement.

TempFileName = ActiveSheet.Name & "-" & Format(Now, "dd-mmm-yy h-mm-ss") & ".pdf"

'Complete path of the file where it is saved
FileFullPath = TempFilePath & TempFileName

'Now Export the Activesshet as PDF with the given File Name and path

 On Error GoTo err
With ActiveSheet
    .ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=FileFullPath, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False
End With

'Now open a new mail

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
.To = StrToReceipent
.CC = StrCCReceipent
.BCC = StrBCCReceipent
.Subject = StrSubject
.Body = StrBody
    .Attachments.Add FileFullPath '--- full path of the pdf where it is saved
    .Send   'or use .Display to show you the email before sending it.
    .Display
End With
On Error GoTo 0

'Since mail has been sent with the attachment
'Now delete the pdf file from the temp folder

Kill FileFullPath

'set nothing to the objects created
Set OutMail = Nothing
Set OutApp = Nothing

'Now set the application properties back to true
With Application
    .ScreenUpdating = True
    .EnableEvents = True
End With
MsgBox ("Email has been Sent Successfully")
Exit Sub
err:
    MsgBox err.Description

End Sub

【讨论】:

以上是关于在 Send 方法失​​败的情况下使用 Outlook 发送邮件的主要内容,如果未能解决你的问题,请参考以下文章

模拟 - 测试是否在不指定参数的情况下调用方法

npm 的“跳过失败的可选依赖项”是啥意思?

自动化测试用例设计的原则

document.activeElement 过滤选择文件弹窗导致的页面失焦

JMS学习四(ActiveMQ消息过滤)

信号中断阻塞模式下的发送方法