[英]Outlook mail intermittently not sent using Excel VBA
I have an intermittent Excel VBA problem when sending via Outlook.通过 Outlook 发送时,我遇到间歇性 Excel VBA 问题。
20 or 30 times a day my clients send email files.我的客户每天发送 20 或 30 次 email 个文件。 About once a week the email doesn't send.
email 大约每周一次不发送。 It's not in the Outbox or Sent.
它不在发件箱或已发送中。 We only find out when we receive a physical sample without the corresponding email, usually the next day.
我们只有在收到没有对应email的实物样品时才会发现,通常是第二天。
The relevant Excel VBA code:相关Excel VBA代码:
'[bunch of code that sets everything up in order to send]
Err.Clear
On Error Resume Next
Set OutMail = oOutlook.CreateItem(0) ' [oOutlook is a previously created object which is an instance of Outlook]
With OutMail
.To = Address ' [Address=email I have pre-filled]
.Subject = "Swabs File"
.body = vbAns ' [vbAns = variant I have pre-filled]
.Attachments.Add (Dir) ' [Dir = string which is the name of the file]
.send
End With
'Error trap if there is any problem at all
If Err Then
MsgBox ("Problem with Outlook - Failed to Send. Please try again")
Exit Sub
else
MsgBox ("Your file has been sent")
End If
When the problem occurs the error isn't trapped, and the client thinks they have successfully sent the file.当问题发生时,错误并没有被捕获,客户端认为他们已经成功发送了文件。
How can I trap the error?我怎样才能捕获错误?
Option Explicit
Private Sub test()
Dim oOutlook As Object
Dim OutMail As Object
' *** with Outlook ***
' As many times as you want there will only be one
'Set oOutlook = New outlook.Application
'On Error Resume Next ' generate mysterious failure,
' hide error so developer cannot debug
On Error GoTo trap ' Error trap if there is any problem at all
If Not oOutlook Is Nothing Then ' confirm Outlook is available
Set OutMail = oOutlook.CreateItem(0)
With OutMail
'.To = Address ' [Address=email I have pre-filled]
.Subject = "Swabs File"
'.body = vbAns ' [vbAns = variant I have pre-filled]
Error 5 ' for demonstration
.Attachments.Add (dir) ' [Dir = string which is the name of the file]
.Display '.Send
End With
MsgBox "Your file has been sent"
Else
MsgBox "oOutlook object does not exist - Failed to Send."
End If
Exit Sub ' <--- bypass the trap if no error
trap: 'Error trap if there is any problem at all
MsgBox "Problem with Outlook - Failed to Send. Please try again"
End Sub
Only use On Error Resume Next
when " You know why the error occurs. "只有在“您知道错误发生的原因”时才使用
On Error Resume Next
。
You can either take steps to address the error or ignore it on purpose.您可以采取措施解决错误或故意忽略它。
There is a general method of using On Error Resume Next
.有一种使用
On Error Resume Next
的通用方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.