excel - 使用 VBA 从 Excel 发送多封带有信息作为邮件正文中的图像的 Outlook 电子邮件时出现问题
问题描述
我正在使用 Excel 文件通过 VBA 将特定信息通过电子邮件发送给收件人。
发送电子邮件后,我发送的项目中的图像在不同的电子邮件之间交换,并且不断发生。
例如,我已向收件人发送了包含与C1
区域相关的数据的图像,C1
并且C1
收件人正确接收了它,但是我发送的项目中的数据图像并由另一个 cc 收件人接收,每次我们打开以检查它时,都会不断更改为C2
, 。C3
C4
发送的邮件显示 C1 的正确数据
发送邮件显示 C3 数据错误
Sub C_1()
'Ron de Bruin, 25-10-2019
'This macro use the function named : CopyRangeToJPG
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim MakeJPG As String
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
strbody = "Dear Sir" & "<br><br>" & _
"Kindly find the retails performance dashboard for your reference." & "<br><br>" & _
"Regards" & "<br>" & _
"Manish Lengade<br>"
'Create JPG file of the range
'Only enter the Sheet name and the range address
MakeJPG = CopyRangeToJPG("Sheet 1", "B2:L15")
If MakeJPG = "" Then
MsgBox "Something go wrong, we can't create the mail"
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Exit Sub
End If
On Error Resume Next
With OutMail
.To = "manish.lengade@maruti.co.in"
.CC = ""
.BCC = ""
.Subject = "Retails Performance Dashboard"
.Attachments.Add MakeJPG, 1, 1
'Note: Change the width and height as needed
.HTMLBody = "<html><p>" & strbody & "</p><img src=""cid:NamePicture.jpg"" width=1000 height=450></html>"
.Display 'or use .Send
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
Function CopyRangeToJPG(NameWorksheet As String, RangeAddress As String) As String
'Ron de Bruin, 25-10-2019
Dim PictureRange As Range
With ActiveWorkbook
On Error Resume Next
.Worksheets(NameWorksheet).Activate
Set PictureRange = .Worksheets(NameWorksheet).Range(RangeAddress)
If PictureRange Is Nothing Then
MsgBox "Sorry this is not a correct range"
On Error GoTo 0
Exit Function
End If
PictureRange.CopyPicture
With .Worksheets(NameWorksheet).ChartObjects.Add(PictureRange.Left, PictureRange.Top, PictureRange.Width, PictureRange.Height)
.Activate
.Chart.Paste
.Chart.Export Environ$("temp") & Application.PathSeparator & "NamePicture.jpg", "JPG"
End With
.Worksheets(NameWorksheet).ChartObjects(.Worksheets(NameWorksheet).ChartObjects.Count).Delete
End With
CopyRangeToJPG = Environ$("temp") & Application.PathSeparator & "NamePicture.jpg"
Set PictureRange = Nothing
End Function
解决方案
在发送电子邮件后删除图像是有意义的。该CopyRangeToJPG
方法将生成的图表作为图像保存到磁盘,但在发送电子邮件后,图像仍留在磁盘上。
此外,如果您想将相同的图像附加到电子邮件中,检查临时文件夹是否已经包含生成的早期图像是有意义的。
无论如何,我建议在调试器下运行代码,逐行遍历代码。
推荐阅读
- python - 如何将字典中键的值打印为列表?
- mysql - RPAD mysql 功能
- javascript - 无法读取未定义的属性“__isVuelidateAsyncVm”
- javascript - 如何将数据从 node.js 传输到另一台服务器上的 JavaScript 客户端?
- ios - 拖动使用 Vstack 和 Hstack 创建的网格的单个单元格
- c++ - TCP/IP 套接字接收随机大数
- python - 如何获得数字列表的平均值
- android - RecyclerView 未显示在 Android Studio 预览版中
- python - pytest python 石墨烯订阅
- python - 如何保存许多numpy数组及其名称