我需要的是给我的同事发电子邮件,在Excel中的某些单元格(从A列到Q列),这些单元格在我的电子邮件中显示为图片。下面是我的代码。但是,我电子邮件草稿中的图片是空白的。它没有显示细胞。有趣的是图片(包含我需要的单元格)被复制到我的剪贴板中,我可以删除空白图片并单击粘贴。但我想使它更加自动化,因为这个宏最终将在整个部门中可用。有人能帮我吗?
Sub CAS_Reminder()
Dim OutApp As Object
Dim OutMail As Object
Dim Rng As Range
Dim LastRow As Long
Dim TempFilePath As String
Dim TempFileName As String
Dim Recipient_Name As String
Dim StringBody As String
Dim Manager_Name As String
Dim RngHeight As Long
Dim RngWidth As Long
'set last row
LastRow = Range("A1").End(xlDown).Row
' Set the range to be copied
Set Rng = Range("A1", "Q" & LastRow)
' Copy the range and paste as picture
Rng.CopyPicture xlScreen, xlPicture
' Create a temporary file to hold the image
TempFilePath = Environ$("temp") & "\"
TempFileName = "SelectedRanges.png"
' Save the image to the temporary file
With ActiveSheet.ChartObjects.Add(0, 0, Rng.Width, Rng.Height)
.Chart.Paste
.Chart.Export FileName:=TempFilePath & TempFileName, FilterName:="PNG"
.Delete
End With
' Store range dimensions in variables
RngHeight = Rng.Height
RngWidth = Rng.Width
' Create a new email
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
' Set the recipients
Recipient_Name = Range("Q2").Value & "@harriscomputer.com"
Manager_Name = Range("D2").Value & "@harriscomputer.com"
OutMail.To = Recipient_Name
OutMail.CC = Manager_Name
' set subject of the email
OutMail.Subject = "xxx"
'set the body of the email
StringBody = "xxx" & _
"<img src='cid:SelectedRanges.png' height='" & RngHeight & "' width='" & RngWidth & "'>"
OutMail.HTMLBody = StringBody
OutMail.Attachments.Add TempFilePath & TempFileName, 1, 0
OutMail.Display
' Clean up
Kill TempFilePath & TempFileName
Set OutMail = Nothing
Set OutApp = Nothing
Sheets(1).Delete
Application.DisplayAlerts = False
End Sub
3条答案
按热度按时间ozxc1zmp1#
似乎单个
.Paste
并不总是有效-有时您需要在图表填充图像之前尝试多次。我在尝试将复制的图像粘贴到工作表时看到过同样的情况(但在这种情况下,失败会生成运行时错误)。例如,参见https://stackoverflow.com/a/60582628/478884这对我来说非常一致:
vmpqdwk32#
您的代码指定了
cid
属性,但从未将附件上的PR_ATTACH_CONTENT_ID
属性设置为匹配值。换一条线
至
2j4z5cfb3#
尝试