简体   繁体   English

使用Excel 2010 vba打开Outlook并发送电子邮件

[英]open outlook with excel 2010 vba and send email

In the below excel 2010 vba I am trying to send an email if outlook is closed. 在下面的excel 2010 vba如果Outlook已关闭,我尝试发送电子邮件。 I do get the confirmation that the email was sent but nothing gets sent. 我确实收到电子邮件已发送但没有任何发送的确认。 If outlook is opened there is no problem, but it may not always be. 如果打开Outlook不会有问题,但不一定总是如此。 Thank you :). 谢谢 :)。

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

Following code opens outlook... 以下代码将打开Outlook ...

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

Try a different test. 尝试其他测试。 Err.Number is likely 429 ActiveX component can't create object. Err.Number可能是429 ActiveX组件无法创建对象。

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

Maybe this: 也许这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM