我正在学习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
[您可以尝试将工作表复制到新工作簿,添加该新工作簿,然后将其删除。
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