vba + Selection.Paste进入outlook +控制位置

时间:2014-07-09 03:28:06

标签: excel vba email excel-vba outlook

我想要做的是将excel中的图表复制到outlook电子邮件中,但经过多次搜索后,我正在努力。

我无法定位粘贴图表的位置。我希望它粘贴在电子邮件正文中的最后一行"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

2 个答案:

答案 0 :(得分:0)

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

    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)

答案 1 :(得分:0)

您也可以尝试使用其他代码(在这种情况下,图像是临时保存在磁盘上):

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

在我的电脑上,第一个代码问我一些许可,第二个代码没有......