将图表粘贴到Excel中的Outlook电子邮件中

时间:2018-03-04 09:17:42

标签: excel vba email outlook outlook-vba

尝试了类似网页上的所有其他代码,但无法正常工作。

这是我目前的版本。仅当我当前有一个新的电子邮件窗口打开时才有效,我的代码会将.body和cell范围详细信息粘贴到2个单独的新电子邮件窗口中。

我只想让代码打开一个新的电子邮件窗口,其中包含内容.body和单元格区域详细信息(包含图表)。任何人都有我的代码出错的想法吗?

Sub pasting01()

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

With OutMail
.TO = "xyz@anc.com"
.CC = "abc@xyz.com"
.Subject = "Test"
.Body = "Dear Mr Lee" & vbNewLine

ActiveSheet.Range("A1:J30").Copy
Set vInspector = OutMail.GetInspector
Set wEditor = vInspector.WordEditor

wEditor.Application.Selection.Start = Len(.Body)
wEditor.Application.Selection.End = wEditor.Application.Selection.Start

wEditor.Application.Selection.Paste

.display

End With

On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing

End Sub

2 个答案:

答案 0 :(得分:1)

为了达到你的目的,你可以搞乱以下事项吗?

Option Explicit

Sub pasting01()

    Dim OutApp As Object
    Dim OutMail As Object

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    Dim myChart As Chart
    Set myChart = ThisWorkbook.Worksheets("Sheet1").ChartObjects("Chart 1").Chart

    Dim myPicture As String
    Dim fileName As String
    Dim myPath As String

    myPicture = "Chart1.png"
    myPath = "C:\Users\User\Desktop\"

    fileName = myPath & myPicture
    myChart.Export fileName

    With OutMail

        .TO = "xyz@anc.com"
        .CC = "abc@xyz.com"
        .Subject = "Test"
        .Body = "Dear Mr Lee" & vbNewLine
        .Attachments.Add fileName
        .HTMLBody = "<html><p>First Line... </p>" & _
                    "<img src=cid:" & Replace(myPicture, " ", "%20") & " height=2*240 width=2*180>" & _
                                                        "<p>Salutation</p>" & _
                                                        "<p>" & "More text" & "</p></html>"
        .Display

    End With

    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing

    Kill fileName

End Sub

结果:

Output

答案 1 :(得分:0)

您的代码存在一些错误,请尝试使用 Option Explicit 模块顶部

Option Explicit
Public Sub pasting01()
    Dim Sht As Excel.Worksheet
    Set Sht = ThisWorkbook.ActiveSheet

    Dim rng As Range
    Set rng = Sht.Range("A1:J30")
        rng.Copy

    Dim OutApp As Object
    Set OutApp = CreateObject("Outlook.Application")

    Dim OutMail As Object
    Set OutMail = OutApp.CreateItem(0)

    Dim vInspector As Object
    Set vInspector = OutMail.GetInspector

    Dim wEditor As Object
    Set wEditor = vInspector.WordEditor

    With OutMail
        .TO = "xyz@anc.com"
        .CC = "abc@xyz.com"
        .Subject = "Test"
        .display

         wEditor.Paragraphs(1).Range.Text = "Dear Mr Lee" & vbCr

         wEditor.Paragraphs(2).Range.Paste

    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub