Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views4 pages

Yes, You Can Use A VBA Macro I

This document provides a VBA macro code for Excel that allows users to copy a specific range from a worksheet and send it via email using Outlook. The macro includes steps for defining the worksheet and range, creating a new email, and customizing the recipient and message. Additionally, it includes a function to convert the copied range into HTML format for inclusion in the email body.

Uploaded by

Swetha R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Yes, You Can Use A VBA Macro I

This document provides a VBA macro code for Excel that allows users to copy a specific range from a worksheet and send it via email using Outlook. The macro includes steps for defining the worksheet and range, creating a new email, and customizing the recipient and message. Additionally, it includes a function to convert the copied range into HTML format for inclusion in the email body.

Uploaded by

Swetha R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Yes, you can use a VBA macro in Excel to copy content from a

worksheet and send it via email using Outlook. Below is a sample


macro code that:

1. Copies a specific range from Excel.

2. Opens a new Outlook email.

3. Pastes the copied content into the body of the email (as a
table).

4. Allows you to customize the recipient, subject, and message.

---

✅ Excel to Outlook Email Macro (with Range as Table)

Sub SendRangeToOutlookEmail()
Dim OutlookApp As Object
Dim OutlookMail As Object
Dim ws As Worksheet
Dim rng As Range
Dim strBody As String

' Define your worksheet and range


Set ws = ThisWorkbook.Sheets("Sheet1") ' Change the sheet
name as needed
Set rng = ws.Range("A1:D10") ' Change the range as
needed

' Copy the range as HTML


rng.Copy

' Create Outlook application


On Error Resume Next
Set OutlookApp = GetObject(, "Outlook.Application")
If OutlookApp Is Nothing Then
Set OutlookApp = CreateObject("Outlook.Application")
End If
On Error GoTo 0

' Create new mail item


Set OutlookMail = OutlookApp.CreateItem(0)

With OutlookMail
.To = "[email protected]" ' Change recipient
.CC = "" ' Optional
.BCC = "" ' Optional
.Subject = "Excel Data"
.HTMLBody = "<p>Hello,</p>" & _
"<p>Please find the data below:</p><br>" & _
RangetoHTML(rng) & _
"<br><p>Regards,<br>Your Name</p>"
.Display ' Use .Send to send automatically
End With

' Cleanup
Set OutlookMail = Nothing
Set OutlookApp = Nothing
End Sub

' Function to convert range to HTML format


Function RangetoHTML(rng As Range) As String
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim wb As Workbook

TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-


yy_hh-mm-ss") & ".htm"

' Copy the range to a new workbook


rng.Copy
Set wb = Workbooks.Add(1)
wb.Sheets(1).Cells(1, 1).PasteSpecial Paste:=xlPasteAll
wb.SaveAs Filename:=TempFile, FileFormat:=xlHtml
wb.Close SaveChanges:=False
' Read all the file text
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.ReadAll
ts.Close

' Delete the temp file


Kill TempFile

Set ts = Nothing
Set fso = Nothing
Set wb = Nothing
End Function

---

🔧 How to Use:

1. Open Excel.

2. Press Alt + F11 to open the VBA Editor.

3. Insert a new module: Insert > Module.

4. Paste the code.

5. Modify:

"Sheet1" to your sheet name.

"A1:D10" to your range.

"[email protected]" to your recipient's email.

6. Press F5 or run the macro.


---

Would you like a version that attaches the Excel file instead of
embedding the content?

You might also like