如何调整代码以仅发送活动工作表或选择的工作搜索而不发送附件中的整个工作簿

问题描述 投票:0回答:1

我正在学习VBA,并试图创建宏以将表格发送到特定的电子邮件。我试图用ThisWorkbook.worksheets(“ Form”)替换thisWorkbook,但这没用。

有人可以帮我吗?

 Sub send_mail()
Dim outlookOBJ As Object
Dim mItem As Object
Set outlookOBJ = CreateObject("Outlook.application")
Set mItem = outlookOBJ.createItem(olMailItem)
With mItem
.to = "[email protected]"
.Subject = "test"
.body = "test"
.attachments.Add ThisWorkbook.path & "\" & ThisWorkbook.Name
.display
End With

End Sub
excel vba email-attachments
1个回答
0
投票

[您可以尝试将工作表复制到新工作簿,添加该新工作簿,然后将其删除。

Dim outlookOBJ As Object
Dim mItem As Object
Set outlookOBJ = CreateObject("Outlook.application")
Set mItem = outlookOBJ.createItem(olMailItem)


Dim TempPath As String
TempPath = "C:\Temp\TempWorkbook.xlsx" 'a path where will save a temporary workbook

ThisWorkbook.Worksheets("Form").Copy 'this will copy the worksheet FORM to a NEW workbook. That new WB will be the active one
ActiveWorkbook.SaveAs TempPath
ActiveWorkbook.Close False

With mItem
    .to = "[email protected]"
    .Subject = "test"
    .body = "test"
    .attachments.Add TempPath
    Kill TempPath 'we delete the temporary Workbook
    .display
End With
© www.soinside.com 2019 - 2024. All rights reserved.