我有以下代码发送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
任何人都可以帮助我,即使在前景关闭时如何使它工作?
答案 0 :(得分: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
答案 1 :(得分:0)
首先你有任何错误处理吗?例如,如果您调用getObject并且它已关闭,那么您应该收到错误吗?
所以大多数人使用的方式是调用get对象,如果出现错误,那么他们知道outlook已经关闭,他们会创建一个新实例。
如果您想要非常精确,错误代码编号为429,请参阅此处的代码Link to previous question.
为了让你开始,这也应该有效
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没有运行。
答案 2 :(得分:0)
最简单的方法是构建一个mailto://
网址并通过shell command运行。
您必须URL encode主题和正文才能正常显示。
示例(在Windows中粘贴在“运行”命令中):
mailto://user@domain.com?subject=New%20Email&body=This%20is%20the%20message%20body.