我正在尝试创建工作表的副本并仅将副本作为附件通过电子邮件发送。它不会附加ActiveWorkbook,我认为这是因为它从未保存过,所以它在一个临时位置。
如果我使用 Activeworkbook.Path
,它给出了文件名,而不是完整路径。
如果我使用 ThisWorkbook.Path
它给出了我从中创建副本的工作簿的路径,而不是副本本身。
Sub DemandEM()
Dim OutApp As Object
Dim Outmail As Object
Dim Subject As String
Dim Body As String
Dim Attachment As String
Subject = "DMND NP" & Sheets("Loading").Cells(4, 2).Value
Body = "Please see attachment for NP" & Sheets("Loading").Cells(4, 2).Value
Sheets("Demand Input").Copy
Attachment = ActiveWorkbook.Path
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set Outmail = OutApp.CreateItem(0)
With Outmail
.to = "ABC@123.com"
.Subject = Subject
.Body = Body
.Attachments.Add (Attachment)
.Display
End With
End Sub
最佳答案
Outlook:发送新创建的工作簿
ThisWorkbook's
中。名称下的路径Demand Input.xlsx
并关闭它。在剩下的 Outlook 代码之后,它将删除该文件。 Option Explicit
Sub DemandEM()
Dim OutApp As Object
Dim Outmail As Object
Dim Subject As String
Dim Body As String
Dim dFilePath As String
Subject = "DMND NP" & Sheets("Loading").Cells(4, 2).Value
Body = "Please see attachment for NP" & Sheets("Loading").Cells(4, 2).Value
ThisWorkbook.Sheets("Demand Input").Copy ' create new one-sheet workbook
With Workbooks(Workbooks.Count)
dFilePath = ThisWorkbook.Path & "\" & "Demand Input.xlsx"
Application.DisplayAlerts = False ' overwrite without confirmation
.Save dFilePath
Application.DisplayAlerts = True
.Close SaveChanges:=False
End With
Set OutApp = CreateObject("Outlook.Application")
OutApp.Session.Logon
Set Outmail = OutApp.CreateItem(0)
With Outmail
.to = "ABC@123.com"
.Subject = Subject
.Body = Body
.Attachments.Add dFilePath
.Display
End With
' If you want to keep the file, out-comment the following line.
Kill dFilePath
End Sub
关于excel - 获取未保存文件的完整路径以附加到 Outlook 邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71530237/