【问题标题】:Pasting Chart into Outlook Email from Excel将图表从 Excel 粘贴到 Outlook 电子邮件中
【发布时间】:2020-06-24 17:27:24
【问题描述】:

尝试了类似页面上的所有其他代码,但均无效。

这是我目前的版本。仅当我当前打开了一个新的电子邮件窗口时才有效,奇怪的是,我的代码会将 .body 和单元格范围的详细信息粘贴到 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

【问题讨论】:

标签: excel vba email outlook


【解决方案1】:

您的代码有一些错误,请尝试使用模块顶部的 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

【讨论】:

  • 非常感谢代码与显式选项完美配合!
【解决方案2】:

你可以根据你的目的搞乱以下内容吗?

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

结果:

【讨论】:

    猜你喜欢
    • 2021-09-27
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-13
    • 2017-11-25
    • 2019-08-02
    相关资源
    最近更新 更多