excel - 获取未保存文件的完整路径以附加到 Outlook 邮件

标签 excel vba outlook

我正在尝试创建工作表的副本并仅将副本作为附件通过电子邮件发送。它不会附加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/

    相关文章:

    .net - COM Interop无需再充气

    excel - Excel 中的失败概率

    excel - 基于多变量的 VBA 案例选择

    excel - 选择变量范围并将空白字段替换为值 "NA"

    c# - 如何为 Outlook 添加身份验证到 ASP.NET 托管的 ICS iCalendar

    email - 我们如何获得 Outlook 收件人的实际电子邮件地址?

    带有字节变量的 VBA 溢出错误

    excel - 在文本框中插入日期 - VBA

    vba - 在vba中用两个函数编写一个文件

    vba - VLOOKUP 的 FOR 循环在两个单元格中返回值并打印第三个单元格