我有一些VBA代码,我在excel中运行,根据主题名称生成电子邮件并将excel文件附加到电子邮件中。对于101封电子邮件,宏似乎运行正常,然后几乎100%的时间都失败了。每个附件都是15kb,创建的电子邮件总数会有所不同,但是对于测试我总共有128个。
实际的电子邮件撰写是电子邮件的正文,附加了默认签名,主题是静态的,并且是可变的。
我无法识别代码所需的任何修改,我每次迭代都会丢弃到OAMail项目,所以我有点亏本(这是看似错误的标准问题) 。
以下代码:
Sub Generate_Emails()
Dim OApp As Object
Dim OMail As Object
Dim signature As String
Dim emailbody As String
Dim ET As Worksheet
Dim Sum_WS As Worksheet
Dim EL As Worksheet
Dim CS As Worksheet
Set ET = ActiveWorkbook.Worksheets("EmailTemplate")
Set Sum_WS = ActiveWorkbook.Worksheets("Summary")
Set EL = ActiveWorkbook.Worksheets("EmailList")
Set CS = ActiveWorkbook.Worksheets("ControlSheet")
Set OApp = CreateObject("Outlook.Application")
'Check if emails can be generated
If CS.Range("F2") = "No" Then
MsgBox "Cannot generate files until Files have been generated", vbExclamation
Exit Sub
Else
i = Application.WorksheetFunction.CountA(EL.Range("A:A"))
body = ET.Range("A1")
'Go through each email in email list
For j = 2 To i
'Create email object
Set OMail = OApp.CreateItem(0)
'Get default signature
With OMail
.GetInspector
End With
'Allocate signature and body
signature = OMail.HTMLBody
'Create the whole email and add attachment
With OMail
.To = EL.Cells(j, 2)
.Subject = "SOx RemTP Audit " & Sum_WS.Range("C2")
.HTMLBody = body & vbNewLine & signature
.Attachments.Add Sum_WS.Range("B2") & "\SOx RemTP Audit " & Sum_WS.Range("C2") & " - " & EL.Cells(j, 1) & ".xlsx"
.Save
End With
Application.StatusBar = "Generating Email " & j & " of " & i
DoEvents
Set OMail = Nothing
Next j
Application.StatusBar = False
End If
Set OApp = Nothing
MsgBox "All emails placed into Outlook draft folder", vbInformation
End Sub
非常感谢任何协助。
干杯
答案 0 :(得分:0)
一些注意事项:您的代码看起来不错但使用Option Explicit
。
在以'##
...
Option Explicit '## force proper variable declare to avoid typos and issues
Public Sub Generate_Emails()
Dim OApp As Object
Dim OMail As Object
Dim signature As String
Dim emailbody As String
Dim ET As Worksheet
Dim Sum_WS As Worksheet
Dim EL As Worksheet
Dim CS As Worksheet
Set ET = ActiveWorkbook.Worksheets("EmailTemplate")
Set Sum_WS = ActiveWorkbook.Worksheets("Summary")
Set EL = ActiveWorkbook.Worksheets("EmailList")
Set CS = ActiveWorkbook.Worksheets("ControlSheet")
Set OApp = CreateObject("Outlook.Application")
'Check if emails can be generated
If CS.Range("F2") = "No" Then
MsgBox "Cannot generate files until Files have been generated", vbExclamation
Exit Sub
Else
Dim i As Long '## dim i
i = Application.WorksheetFunction.CountA(EL.Range("A:A"))
emailbody = ET.Range("A1")
'Go through each email in email list
Dim j As Long '## dim j
For j = 2 To i
'Create email object
Set OMail = OApp.CreateItem(0)
'Get default signature
With OMail '## tidied up your with block (one is enough)
.GetInspector
'Allocate signature and body
signature = .HTMLBody
'Create the whole email and add attachment
.To = EL.Cells(j, 2)
.Subject = "SOx RemTP Audit " & Sum_WS.Range("C2")
.HTMLBody = emailbody & vbNewLine & signature
.Attachments.Add Sum_WS.Range("B2") & "\SOx RemTP Audit " & Sum_WS.Range("C2") & " - " & EL.Cells(j, 1) & ".xlsx"
.Save
.Close 0 '## close the mail to not leave it open (this might be the issue)
'0=olSave; 1=olDiscard; 2=olPromptForSave
End With
Application.StatusBar = "Generating Email " & j & " of " & i
DoEvents
'Set OMail = Nothing '## not needed
Next j
Application.StatusBar = False
End If
'Set OApp = Nothing '## not needed
MsgBox "All emails placed into Outlook draft folder", vbInformation
End Sub
几乎不需要 Set Something = Nothing
,因为VBA会在End Sub
上自动执行此操作。
答案 1 :(得分:0)
通过在With语句中添加“.close 0”来解决问题。
原始循环:
'Go through each email in email list
For j = 2 To i
'Create email object
Set OMail = OApp.CreateItem(0)
'Get default signature
With OMail
.GetInspector
End With
'Allocate signature and body
signature = OMail.HTMLBody
'Create the whole email and add attachment
With OMail
.To = EL.Cells(j, 2)
.Subject = "SOx RemTP Audit " & Sum_WS.Range("C2")
.HTMLBody = body & vbNewLine & signature
.Attachments.Add Sum_WS.Range("B2") & "\SOx RemTP Audit " & Sum_WS.Range("C2") & " - " & EL.Cells(j, 1) & ".xlsx"
.Save
End With
Application.StatusBar = "Generating Email " & j & " of " & i
DoEvents
Set OMail = Nothing
Next j
在添加.close 0并合并with:
之后修改循环 'Go through each email in email list
For j = 2 To i
'Create email object
Set OMail = OApp.CreateItem(0)
'Get default signature
With OMail
.GetInspector
'Allocate signature
signature = OMail.HTMLBody
'Create the whole email and add attachment
.To = EL.Cells(j, 2)
.Subject = emailsubject
.HTMLBody = emailbody & vbNewLine & signature
.Attachments.Add attachmentsfolder & EL.Cells(j, 1) & ".xlsx"
.Save
.Close 0
End With
Application.StatusBar = "Generating Email " & j & " of " & i
DoEvents
Set OMail = Nothing
Next j
感谢Peh和Sam提供解决方案