附加未保存的工作簿

时间:2017-10-09 07:49:49

标签: excel vba excel-vba email

我创建了一个宏来发送自动电子邮件。

附件出错。我自己创建附件。

我想要的附件是工作表ws1,从博客中读取,应该是整个工作簿(文件)作为附件。

我不保存我创建的工作簿。我希望在发送邮件后删除创建的工作簿,这就是最后一段代码的原因。

Sub EmailsNewReport()

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.EnableEvents = False

Dim ws1 As Worksheet
Dim wb As Workbook
Dim ws2 As Worksheet

Set ws1 = ThisWorkbook.Worksheets("New Report")
Set wb = Workbooks.Add
Set ws2 = wb.Worksheets(1)

ws1.Cells.Copy
With ws2
    .Cells.PasteSpecial xlValues
    .Cells.PasteSpecial xlFormats
End With

ws1.Activate
ToArray = "aaa@gmail.com"
Subject = "Hello"
Content = "Hey"
ws1.Range("B3:P31").Select
ActiveWorkbook.EnvelopeVisible = True
With Sheets("New Report").MailEnvelope
    .Introduction = Content
    .Item.To = ToArray
    .Item.Subject = Subject
    .Item.attachments.Add (wb.FullName)
    .Item.Send
End With
Application.DisplayAlerts = False

ws2.Parent.Close False
Set ws2 = Nothing

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True

End Sub

1 个答案:

答案 0 :(得分:0)

您可以暂时将工作簿保存在某处,并使用完整路径附加文件,并在发送带附件的邮件后删除该文件。