问题描述
很抱歉打扰,但我真的坚持使用此代码,因为我对 vba 编码完全陌生。我想了解更多,但我无法弄清楚以下代码缺少什么导致下面失败。它可以显示第一个草稿电子邮件,但无法显示第二个或下一个草稿电子邮件。感谢是否可以帮助修复代码?谢谢!该错误一直显示我卡在“ myAttachments.Add(附件)”中。不知道是为什么?
Sub autosend()
Dim datasheet As Worksheet
Dim reportsheet As Worksheet
Dim accountnum As String
Dim finalrow As Integer
Dim i As Integer
Dim edress As String
Dim subj As String
Dim message As String
Dim filename As String
Dim outlookapp As Object
Dim outlookmailitem As Object
Dim myAttachments As Object
Dim path As String
Dim attachment As String
Dim xnum As Long ' x is for the loop
Dim LR As Long 'LR means Last Row
Set erange = Sheet3.Range("A2:B9999")
Set datasheet = Sheet1
Set reportsheet = Sheet2
' here can start the loop ->
LR = ActiveSheet.UsedRange.Rows.Count
For xnum = 2 To LR
accountnum = Sheet3.Range("D" & xnum).Value 'set the email to column customer name
edress = Application.WorksheetFunction.VLookup(accountnum,erange,2,False)
Sheet3.Range("E" & xnum).Value = edress ' set the email address is at this range to read
reportsheet.Range("A9:N1000").ClearContents ' clear content as may have prevIoUs record
datasheet.Select
finalrow = Cells(Rows.Count,1).End(xlUp).Row
For i = 3 To finalrow
If Cells(i,4) = accountnum Then
Range(Cells(i,1),Cells(i,10)).copy
reportsheet.Select
Range("A200").End(xlUp).Offset(1,0).PasteSpecial xlPasteFormulasAndNumberFormats
datasheet.Select
End If
Next i
reportsheet.Select 'use this sheet to make it as an attachment
Set outlookapp = CreateObject("outlook.application")
Set outlookmailitem = outlookapp.CreateItem(0)
Set myAttachments = outlookmailitem.Attachments
Application.displayAlerts = False
filename = accountnum & ".xlsx" 'file naming
subj = accountnum
Worksheets("sheet2").copy ' save it as an excel format
With ActiveWorkbook
.SaveAs filename:=accountnum,FileFormat:=xlOpenXMLWorkbook
.Close savechanges:=False
End With
attachment = accountnum & ".xlsx" ' attachment naming,which is duplicate with above but giving
attachment a referrence.
outlookmailitem.To = edress
outlookmailitem.cc = ""
outlookmailitem.bcc = ""
outlookmailitem.Subject = "" & subj
outlookmailitem.body = ""
myAttachments.Add (attachment)
outlookmailitem.display
Application.displayAlerts = True
Set outlookapp = nothing
Set outlookmailitem = nothing
Range("A1").Select
Next xnum
End Sub
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)