开放的前景与Excel 2010 vba和发送电子邮件

在下面的excel 2010 vba我试图发送一封电子邮件,如果outlook是closures的。 我得到确认电子邮件发送,但没有发送。 如果outlook是没有问题的,但并不总是如此。 谢谢 :)。

 Option Explicit Private Sub CommandButton21_Click() Dim OutApp As Object Dim OutMail As Object Dim strbody As String Dim oOutlook As Object On Error Resume Next Set oOutlook = GetObject(, "Outlook.Application") On Error GoTo 0 If oOutlook Is Nothing Then Set oOutlook = CreateObject("Outlook.Application") End If strbody = "Hi xxxx," & vbNewLine & vbNewLine & _ "There are 4 reports ready" & vbNewLine & _ "Regards" & vbNewLine & vbNewLine & _ "xxxxx xxxxx" On Error Resume Next With OutMail .To = "xxxxx@xxxxxxx.com" .CC = "" .BCC = "" .Subject = "data" .Body = strbody '.Attachments.Add ("C:\test.txt") .Send End With On Error GoTo 0 Set OutMail = Nothing Set OutApp = Nothing ' Confirm that the email(s) has/have been sent MsgBox "The data has been emailed sucessfully.", vbInformation End Sub 

以下代码打开Outlook …

 Application.ActivateMicrosoftApp xlMicrosoftMail Application.Wait(Now + TimeValue("00:00:03")) 

尝试一个不同的testing。 Err.Number很有可能是429的ActiveX组件不能创build对象。

 On Error Resume Next Set oOutlook = GetObject(, "Outlook.Application") On Error GoTo 0 If Err.Number <> 0 Then Set oOutlook = CreateObject("Outlook.Application") End If 

也许这个:

 Set oOutlook = Nothing On Error Resume Next Set oOutlook = GetObject(, "Outlook.Application") On Error GoTo 0 If oOutlook Is Nothing Then Set oOutlook = CreateObject("Outlook.Application") End If