Làm cách nào để dán một dải ô vào nội dung thư dưới dạng hình ảnh trong Excel?
Nếu bạn cần sao chép một dải ô và dán nó dưới dạng hình ảnh vào nội dung thư khi bạn gửi email từ Excel. Làm thế nào bạn có thể đối phó với nhiệm vụ này?
Dán một dải ô vào nội dung email dưới dạng hình ảnh với mã VBA trong Excel
Dán một dải ô vào nội dung email dưới dạng hình ảnh với mã VBA trong Excel
Có thể không có phương pháp tốt nào khác để bạn giải quyết công việc này, một đoạn mã VBA trong bài viết này có thể giúp bạn. Vui lòng làm như sau:
1. Bật trang tính bạn muốn sao chép và dán các ô dưới dạng hình ảnh, nhấn và giữ ALT + F11 phím để mở Microsoft Visual Basic cho các ứng dụng cửa sổ.
2. Nhấp chuột Chèn > Mô-đunvà dán mã sau vào Mô-đun Cửa sổ.
Mã VBA: dán một dải ô vào nội dung email dưới dạng hình ảnh:
Sub sendMail()
Dim TempFilePath As String
Dim xOutApp As Object
Dim xOutMail As Object
Dim xHTMLBody As String
Dim xRg As Range
On Error Resume Next
Set xRg = Application.InputBox("Please select the data range:", "KuTools for Excel", Selection.Address, , , , , 8)
If xRg Is Nothing Then Exit Sub
With Application
.Calculation = xlManual
.ScreenUpdating = False
.EnableEvents = False
End With
Set xOutApp = CreateObject("outlook.application")
Set xOutMail = xOutApp.CreateItem(olMailItem)
Call createJpg(ActiveSheet.Name, xRg.Address, "DashboardFile")
TempFilePath = Environ$("temp") & "\"
xHTMLBody = "<span LANG=EN>" _
& "<p class=style2><span LANG=EN><font FACE=Calibri SIZE=3>" _
& "Hello, this is the data range that you want:<br> " _
& "<br>" _
& "<img src='cid:DashboardFile.jpg'>" _
& "<br>Best Regards!</font></span>"
With xOutMail
.Subject = ""
.HTMLBody = xHTMLBody
.Attachments.Add TempFilePath & "DashboardFile.jpg", olByValue
.To = " "
.Cc = " "
.Display
End With
End Sub
Sub createJpg(SheetName As String, xRgAddrss As String, nameFile As String)
Dim xRgPic As Range
Dim xShape As Shape
ThisWorkbook.Activate
Worksheets(SheetName).Activate
Set xRgPic = ThisWorkbook.Worksheets(SheetName).Range(xRgAddrss)
xRgPic.CopyPicture
With ThisWorkbook.Worksheets(SheetName).ChartObjects.Add(xRgPic.Left, xRgPic.Top, xRgPic.Width, xRgPic.Height)
.Activate
For Each xShape In ActiveSheet.Shapes
xShape.Line.Visible = msoFalse
Next
.Chart.Paste
.Chart.Export Environ$("temp") & "\" & nameFile & ".jpg", "JPG"
End With
Worksheets(SheetName).ChartObjects(Worksheets(SheetName).ChartObjects.Count).Delete
Set xRgPic = Nothing
End Sub
Note: In the above code, you can change the body content and email address to your need.
3. After inserting the code, press F5 key to run this code, a dialog box is popped out to remind you selecting the data range that you want to insert into the email body as picture, see screenshot:

4. Then click OK button, and a Message window is displayed, the selected data range has been inserted into the body as image, see screenshot:

Note: In the Message window, you can also change the body content and Email addresses in To and Cc fields as you need.
5. At last, click Send button to send this email.
Note: If you need to paste multiple ranges from different worksheets, the below VBA code can do you a favor:
First, you should select the multiple ranges that you want to insert into the email body as pictures, and then apply the following code:
VBA code: paste multiple ranges of cells into email body as image:
Sub sendMail()
Dim TempFilePath As String
Dim xOutApp As Object
Dim xOutMail As Object
Dim xHTMLBody As String
Dim xRg As Range
Dim xSheet As Worksheet
Dim xAcSheet As Worksheet
Dim xFileName As String
Dim xSrc As String
On Error Resume Next
TempFilePath = Environ$("temp") & "\RangePic\"
If Len(VBA.Dir(TempFilePath, vbDirectory)) = False Then
VBA.MkDir TempFilePath
End If
Set xAcSheet = Application.ActiveSheet
For Each xSheet In Application.Worksheets
xSheet.Activate
Set xRg = xSheet.Application.Selection
If xRg.Cells.Count > 1 Then
Call createJpg(xSheet.Name, xRg.Address, "DashboardFile" & VBA.Trim(VBA.Str(xSheet.Index)))
End If
Next
xAcSheet.Activate
With Application
.Calculation = xlManual
.ScreenUpdating = False
.EnableEvents = False
End With
Set xOutApp = CreateObject("outlook.application")
Set xOutMail = xOutApp.CreateItem(olMailItem)
xSrc = ""
xFileName = Dir(TempFilePath & "*.*")
Do While xFileName <> ""
xSrc = xSrc + VBA.vbCrLf + "<img src='cid:" + xFileName + "'><br>"
xFileName = Dir
If xFileName = "" Then Exit Do
Loop
xHTMLBody = "<span LANG=EN>" _
& "<p class=style2><span LANG=EN><font FACE=Calibri SIZE=3>" _
& "Hello, this is the data range that you want:<br> " _
& "<br>" _
& xSrc _
& "<br>Best Regards!</font></span>"
With xOutMail
.Subject = ""
.HTMLBody = xHTMLBody
xFileName = Dir(TempFilePath & "*.*")
Do While xFileName <> ""
.Attachments.Add TempFilePath & xFileName, olByValue
xFileName = Dir
If xFileName = "" Then Exit Do
Loop
.To = " "
.Cc = " "
.Display
End With
If VBA.Dir(TempFilePath & "*.*") <> "" Then
VBA.Kill TempFilePath & "*.*"
End If
End Sub
Sub createJpg(SheetName As String, xRgAddrss As String, nameFile As String)
Dim xRgPic As Range
ThisWorkbook.Activate
Worksheets(SheetName).Activate
Set xRgPic = ThisWorkbook.Worksheets(SheetName).Range(xRgAddrss)
xRgPic.CopyPicture
With ThisWorkbook.Worksheets(SheetName).ChartObjects.Add(xRgPic.Left, xRgPic.Top, xRgPic.Width, xRgPic.Height)
.Activate
.Chart.Paste
.Chart.Export Environ$("temp") & "\RangePic\" & nameFile & ".jpg", "JPG"
End With
Worksheets(SheetName).ChartObjects(Worksheets(SheetName).ChartObjects.Count).Delete
Set xRgPic = Nothing
End Sub
The Best Office Productivity Tools
Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%
- Reuse: Quickly insert complex formulas, charts and anything that you have used before; Encrypt Cells with password; Create Mailing List and send emails...
- Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
- Merge Cells/Rows/Columns without losing Data; Split Cells Content; Combine Duplicate Rows/Columns... Prevent Duplicate Cells; Compare Ranges...
- Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
- Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
- Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
- Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
- Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
- More than 300 powerful features. Supports Office / Excel 2007-2021 and 365. Supports all languages. Easy deploying in your enterprise or organization. Full features 30-day free trial. 60-day money back guarantee.
Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier
- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
Sort comments by
#25578
This comment was minimized by the moderator on the site
0
0
#28387
This comment was minimized by the moderator on the site
Report
0
0
#28388
This comment was minimized by the moderator on the site
0
0
#28669
This comment was minimized by the moderator on the site
0
0
#28670
This comment was minimized by the moderator on the site
0
0
#28671
This comment was minimized by the moderator on the site
0
0
#29217
This comment was minimized by the moderator on the site
0
0
#29218
This comment was minimized by the moderator on the site
Report
0
0
#29306
This comment was minimized by the moderator on the site
0
0
#29307
This comment was minimized by the moderator on the site
Report
0
0
#29308
This comment was minimized by the moderator on the site
Report
0
0
#30037
This comment was minimized by the moderator on the site
Report
0
0
#30038
This comment was minimized by the moderator on the site
Report
0
0
#30039
This comment was minimized by the moderator on the site
Report
0
0
#30040
This comment was minimized by the moderator on the site
Report
0
0
#30041
This comment was minimized by the moderator on the site
Report
0
0
#30323
This comment was minimized by the moderator on the site
0
0
#30564
This comment was minimized by the moderator on the site
0
0
#30565
This comment was minimized by the moderator on the site
Report
0
0
#30566
This comment was minimized by the moderator on the site
Report
0
0
#30567
This comment was minimized by the moderator on the site
Report
0
0
#30568
This comment was minimized by the moderator on the site
0
0
#30569
This comment was minimized by the moderator on the site
Report
0
0
#30816
This comment was minimized by the moderator on the site
0
0
#31145
This comment was minimized by the moderator on the site
Report
0
0
#31475
This comment was minimized by the moderator on the site
Report
0
0
#31476
This comment was minimized by the moderator on the site
0
0
#31477
This comment was minimized by the moderator on the site
0
0
#31641
This comment was minimized by the moderator on the site
0
0
#31647
This comment was minimized by the moderator on the site
Report
0
0
#32078
This comment was minimized by the moderator on the site
0
0
#32874
This comment was minimized by the moderator on the site
Report
0
0
#34099
This comment was minimized by the moderator on the site
0
0
#34145
This comment was minimized by the moderator on the site
0
0
There are no comments posted here yet
Load More