Hi,
I did some research and found how it is to be done.
- Go to your Excel sheet and open your VBE.
- Create a new module.
- Paste the below code. (please make changes as per your requirements)
- Close the VBA Editor.
- Press Alt+f8 and select the SendExcelDataInExcel macro and click on "Run".
Sub SendExcelDataInEmail()
Dim OutApp As Object
Dim OutMail As Object
Dim MyWorkbook As Workbook
Dim MyWorksheet As Worksheet
Dim StrBody As String
Dim RecipientEmailAddress As String
' Create a reference to the Outlook application
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
' Set the Excel workbook and worksheet
Set MyWorkbook = ThisWorkbook ' Assumes you are working with the active workbook
Set MyWorksheet = MyWorkbook.Sheets("SheetName") ' Replace "SheetName" with the name of your sheet
' Define the email subject
OutMail.Subject = "End of Day Report"
' Add recipients (To, CC, BCC)
RecipientEmailAddress = "recipient@example.com" ' Change this to the recipient's email address
OutMail.To = RecipientEmailAddress
OutMail.CC = "cc@example.com" ' You can add CC recipients
OutMail.BCC = "bcc@example.com" ' You can add BCC recipients
' Define the email body
StrBody = "Hello," & vbCrLf & vbCrLf & _
"Please find the End of Day report attached." & vbCrLf & vbCrLf & _
"Best regards," & vbCrLf & _
"Your Name"
' Add body content to the email
OutMail.Body = StrBody
' Attach the Excel workbook to the email
MyWorkbook.Save ' Save the workbook
MyWorkbook.Close ' Close the workbook
OutMail.Attachments.Add MyWorkbook.FullName
' Display the email and send
OutMail.Display
'OutMail.Send ' Uncomment this line to send the email automatically
' Clean up
Set OutMail = Nothing
Set OutApp = Nothing
End Sub