VBA附加Excel文件而不保存

时间:2017-05-25 12:31:24

标签: excel-vba vba excel

我已创建Excel文件,并希望在Outlook电子邮件中附加相同内容而不保存Excel文件。我可以在文件保存到文件夹后附加文件,但我不想将其保存在任何地方。请协助。

Dim wkb As Workbook
Dim wkb1 As Worksheet

Set wkb = Workbooks.Add
Set wkb1 = Worksheets(1)
wkb1.Name = "Training Tracker"

Dim OutApp As Object
Dim OutMail As Object

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

    With OutMail
        .To = Sheet5.Range("B2").Value
         wkb.Activate
        .Attachments.Add (ActiveWorkbook)
        .Display
    End With

1 个答案:

答案 0 :(得分:0)

您需要在某处保存一份副本才能发送。你可以做的是将它的副本保存到你的临时文件夹。操作系统会定期清除此文件夹,并且不会使用用户文件夹进行干预(适用于Windows而非mac)

Dim wkb As Workbook
Dim wkb1 As Worksheet

Set wkb = Workbooks.Add
Set wkb1 = Worksheets(1)
wkb1.Name = "Training Tracker"

Dim OutApp As Object
Dim OutMail As Object

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

With OutMail
    .To = Sheet5.Range("B2").Value
     wkb.Activate
     wkb.SaveCopyAs Environ("temp") & "\TempFileName.xls"
    .Attachments.Add (Environ("temp") & "\TempFileName.xls")
    .Display
End With