将签名和剪贴板图像添加到电子邮件正文VBA
问题描述:
我试图通过VBA创建一个Outlook消息,其中我想添加一个文本字符串,一些表(使用RangetoHTML函数),一张图片然后邮件签名。将签名和剪贴板图像添加到电子邮件正文VBA
我希望避免保存文件与图片,并通过附接将其添加(往往不是它不正确显示图像),即使用命令:
img src= 'img_name'.jpg
我希望它可以使用下面的代码来完成,但到目前为止,我不能让图像被放置在文本之后和签名之前:
Sub Mail_Selection_Range_Outlook_Body()
'Variables
Dim r1 As Range
Dim r2 As Range
Dim s As String
Dim wordDoc As Word.Document
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
'Get the text that will go on the mail body
s = ActiveWorkbook.Sheets("Plan2").Range("A1")
Set r1 = Sheets("Plan1").Range("A1:D4")
With OutMail
Set wordDoc = OutMail.GetInspector.WordEditor
.To = "[email protected]"
.Subject = "test"
.HTMLbody = s & RangetoHTML(r1) & .HTMLbody
'Set the range that will be pasted as an image
Set r2 = Sheets("Plan1").Range("A5:D9")
r2.CopyPicture Format:=xlPicture
OutMail.Display
'Set the position to paste the image
wordDoc.Application.Selection.Start = currentPosition
wordDoc.Application.Selection.End = wordDoc.Application.Selection.Start
'Paste the image
wordDoc.Application.Selection.Paste
.Close olSave
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
- 如果我使用命令
wordDoc.Application.Selection.Start = currentPosition
图像在文本之前; - 如果我在
wordDoc.Application.Selection.Start = currentPosition
之前放置命令.SendKeys "{END}"
; - 如果我使用
wordDoc.Application.Selection.Start = Len(.Body)
图像放在签名之后!
在此先感谢。
答
实际上,我是能够找到一个解决办法,只是进行一些调整,我的代码:
Sub Mail_Selection_Range_Outlook_Body()
'Variables
Dim r1 As Range
Dim r2 As Range
Dim s1 As String
Dim s2 As String
Dim wordDoc As Word.Document
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
'Get the text that will go on the mail body
s1 = ActiveWorkbook.Sheets("Plan2").Range("A1")
s2 = ActiveWorkbook.Sheets("Plan2").Range("A2")
Set r1 = Sheets("Plan1").Range("A1:D4")
With OutMail
Set wordDoc = OutMail.GetInspector.WordEditor
.To = "[email protected]"
.Subject = "test"
.HTMLbody = s1 & RangetoHTML(r1) & .HTMLbody
'Set the range that will be pasted as an image
Set r2 = Sheets("Plan1").Range("A5:J22")
r2.CopyPicture Format:=xlPicture
OutMail.Display
'Set the position to paste the image
wordDoc.Application.Selection.Start = currentPosition
wordDoc.Application.Selection.End = wordDoc.Application.Selection.Start
'Paste the image
wordDoc.Application.Selection.Paste
.HTMLbody = s2 & .HTMLbody
.Close olSave
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
这样,我仍然能够避免通过附件粘贴图像。
注意:为了将图片后面的文本放在新行中,我只需在参考单元格内的文本(本例中为“Plan2”的“A1”)之前添加"<br>"
即可。
不知道我是否理解,但可能像'Application.Paragraphs.Add()。Paste'在'OutMail.Display'之前(我认为签名是在显示时添加的) – Slai
如果不添加图片作为电子邮件的附件,那么电子邮件将不包含图片,并且图片无法显示(因为它不是电子邮件的一部分)。 **唯一的另一种方法是将图片上传到服务器(将提供给收件人),并在该服务器上包含指向该电子邮件的图片的链接。要将图片附加到电子邮件,请使用以下解决方案/代码:http://stackoverflow.com/questions/32223633/vba-excel-how-can-i-send-an-image-saved-in-excel-on- my-hard-drive-in-an-email/32224724#32224724 – Ralph
请注意,(在上面提供的解决方案中)Outlook忽略将附件仅作为附件显示,因为图片显示在电子邮件正文中。然而,这只是Outlook展示事物的疯狂方式。其他电子邮件客户端会将图片(正确)显示为附件。否则,图片无法显示在电子邮件正文中。通过VBA代码阅读,您还会注意到图片实际上已附加到电子邮件中。再一次,Outlook不会显示图片(无论哪个原因)。 – Ralph