vba + Selection.Paste into outlook + control poition

我正在做的是从Excel复制一个图表到一个Outlook电子邮件,但经过无数的search,我挣扎。

我无法定位粘贴图表的位置。 我希望它在电子邮件正文的最后一行"this is another line again "后粘贴。 它目前粘贴在电子邮件开始之前的行"test ... body"

  Sub CopyAndPasteToMailBody3() ' this works but how do i control where it puts the chart? Set mailApp = CreateObject("Outlook.Application") Set mail = mailApp.CreateItem(olMailItem) mail.Display mail.To = "A@a.com" mail.subject = "subject" & Now mail.body = "test ... body" & vbNewLine & vbNewLine _ & "this is another line " & vbCrLf _ & "this is another line again " Set wEditor = mailApp.ActiveInspector.wordEditor ActiveChart.ChartArea.Copy ' chart needs to be active wEditor.Application.Selection.Paste ' mail.send End Sub 

注意 :在Windows 7上使用Excel 10

我发现了

 Set wEditor = mailapp.ActiveInspector.WordEditor 

需要紧随其后

 wEditor.Range(0, 0).Select 

为了避免错误,有时候你去粘贴它。

您可以修改放在剪贴板上的代码并粘贴:

  Set mailApp = CreateObject("Outlook.Application") Set mail = mailApp.CreateItem(olMailItem) mail.Display mail.To = "A@a.com" mail.Subject = "subject" & Now Dim Clip As MSForms.DataObject Set Clip = New MSForms.DataObject Clip.SetText ("test ... body" & vbNewLine & vbNewLine _ & "this is another line " & vbCrLf _ & "this is another line again " & vbNewLine & " ") Clip.PutInClipboard Set wEditor = mailApp.ActiveInspector.wordEditor wEditor.Application.Selection.Paste ActiveChart.ChartArea.Copy ' chart needs to be active wEditor.Application.Selection.Paste ' mail.send 

在这种情况下,您可以根据需要组装邮件。
MSForms.DataObject需要具有参考: Microsoft Form 2.0对象库(FM20.DLL)

您也可以尝试另一个代码(在这种情况下,图像被临时保存在磁盘上):

 Sub CopyAndPasteToMailBody4() ' this works but how do i control where it puts the chart? Set mailApp = CreateObject("Outlook.Application") Set mail = mailApp.CreateItem(0) mail.Display mail.To = "A@a.com" mail.Subject = "subject" & Now Dim Stri As String Stri = "test ... body" & vbNewLine & vbNewLine _ & "this is another line " & vbCrLf _ & "this is another line again " & vbNewLine & " " ActiveChart.Export "e:\0\C1.png" Stri = Stri & "<img src='e:\0\C1.png'>" mail.HTMLBody = Stri ' mail.send End Sub 

在我的电脑上的第一个代码问我一些权限,第二个代码没有…