我有一个相当大的宏,它在循环内创建操作项列表,确定电子邮件收件人,然后创建/发送带有.xls附件的电子邮件。这是代码的摘要版本:
For i = 0 to tgo_count 'tgo_count set elsewhere, global variable
'reset flags for each loop iteration
high_sev_Flag = False
overdue_Flag = False
'Create Remedy Action Items lists
Call Create_Remedy_Action_Item_Lists(arr_TGO(i))
'Create Assessments Action Items list
Call Create_Assm_Action_Item_Lists(arr_TGO(i))
'Get Recipients for email
Call Get_Recipients(arr_TGO(i))
'Create the product specific attachement to be included on the emails
Call Create_Attachment(arr_TGO(i))
'Create the HTML string to be used in the email body
Create_HTML_Body
'Initialize email objects
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
'rangeToHTML to copy info into an email
'Construct the actual email in outlook
With OutMail
.To = str_recipients
.CC = curr_TCO & ";" & curr_Tech_Lead & ";" & curr_Others
.BCC = ""
.Attachments.Add path & attach_file
.Subject = arr_TGO(i) & " - ITRC Break Report (As of " & Format(Date, "m/d") & ")"
.HTMLBody = "<Body style=font-size:11pt;font-family:Calibri>" & _
"The following tables list all Breaks for " & arr_TGO(i) & ". The data is split by action items and all other breaks. This report was generated using data pulled on " & Format(Date, "m/d") & ". Please follow-up with your TCO if you have any updates on the breaks. <br><br>" & _
str_body & _
"<Body style=font-size:9pt;font-family:Calibri>" & str_sev & str_overdue & "</Body><br><br>The following is historical data for each tracked metric, over the past " & lcol_hist - 1 & " weeks." & _
RangetoHTML(rng_hist, "historical_data")
If email_action = "Display" Then
.display
Else
.send
End If
End With
Next i 'next TGO
'Delete all created attachment files
For i = LBound(arr_TGO) To UBound(arr_TGO)
attach_file = arr_TGO(i) & " - ITRC Break Report (As of " & Format(Date, "m_d") & ").xls"
On Error Resume Next
Kill path & attach_file
On Error GoTo 0
Next i
当email_action
设置为Display
时,程序运行顺畅,永不崩溃。但是,当我希望程序在每次迭代中发送电子邮件时,它总是崩溃(通常在尝试发送9封电子邮件中的第4封时)。每个附件不超过200KB。
有没有办法可以有效地调试这个? Excel不会提供“Excel已停止工作”以外的错误消息。很少,我会得到消息“Excel已经耗尽资源。请关闭其他应用程序等”。如果我在该邮件上发送垃圾邮件,则宏将继续。如果这只是一个资源约束问题,有没有办法可以优化电子邮件的发送以避免这种情况?