我有一些VBA代码,我从excel中运行,它根据主题名称生成电子邮件并将excel文件附加到电子邮件中。宏似乎运行良好的101封电子邮件,然后失败,几乎100%的时间。每个附件是15 to,总数量的电子邮件创建将有所不同,但测试,我有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如能提供任何协助,将不胜感激。
干杯
发布于 2017-09-26 13:51:04
注意:您的代码看起来很好,但是使用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 SubSet Something = Nothing几乎不需要,因为VBA在End Sub上自动执行此操作。
发布于 2017-09-26 14:32:08
通过在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并与以下内容合并后:
'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提供的解决方案
https://stackoverflow.com/questions/46426347
复制相似问题