1

我有以下发送 Outlook 邮件的代码。但是当前景关闭时,这将不起作用。
Sub DraftMail(emailAddr, strBody, strSub)
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = GetObject(, "Outlook.Application")
If OutApp Is Nothing Then
Set OutApp = CreateObject("Outlook.Application")
End If Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
    .To = ""
    .CC = ""
    .BCC = emailAddr
    .Subject = strSub
    .HTMLBody = strBody
    .Send   'or use .Display
    .ReadReceiptRequested = True
End With
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

End Sub

即使前景关闭,任何人都可以帮助我如何使其工作吗?

4

3 回答 3

0

最简单的做法是构造一个mailto://url 并通过shell 命令运行它。

您必须对主题和正文进行URL 编码才能正确显示。

示例(在 Windows 中粘贴到运行命令中):

mailto://user@domain.com?subject=New%20Email&body=This%20is%20the%20message%20body.

结果

于 2013-05-31T16:58:50.390 回答
0

试试这个代码:(未测试)

Sub SendMail()

    Dim iMsg As Object
    Dim iConf As Object
    Dim Flds As Variant


    Set iMsg = CreateObject("CDO.Message")
    Set iConf = CreateObject("CDO.Configuration")

    iConf.Load -1
    Set Flds = iConf.Fields

    With Flds
        .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "<server>"
        .Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
        .Update
    End With

    With iMsg

        Set .Configuration = iConf
        .To = "test@gmail.com"
        .From = "test@gmail.com"
        .Subject = "MIS Reports" & " " & Date & " " & Time
        .TextBody = "Link to Currency Data :" & vbNewLine & "<" & myDest & ">"
        .Send
    End With

    Set iMsg = Nothing
    Set iConf = Nothing

End Sub
于 2013-05-31T09:18:06.783 回答
0

首先,您在那里有任何错误处理吗?例如,如果您调用 getObject 并且它已关闭,您应该得到一个错误?

因此,大多数人使用的方式是调用 get object,如果出现错误,他们就会知道 Outlook 已关闭并创建一个新实例。

如果您想非常精确,错误代码号是 429,请参见此处的代码链接到上一个问题。

为了让你开始,这也应该有效

On Error Resume Next

Set OutApp = GetObject(, "Outlook.Application")
If OutApp Is Nothing Then
   Set OutApp = CreateObject("Outlook.Application")
End If

完成此操作后,您可以删除“On Error Resume Next”并根据需要捕获特定的错误 429,然后您就知道该错误是因为 Outlook 未运行。

于 2013-05-31T13:35:18.867 回答