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

从Excel将图表粘贴到Outlook电子邮件中,excel,vba,email,outlook,Excel,Vba,Email,Outlook,在类似页面上尝试了所有其他代码,但均失败 这是我目前的版本。只有当我当前打开了一个新的电子邮件窗口时,我的代码才会工作。奇怪的是,我的代码会将.body和cell范围的详细信息粘贴到两个单独的新电子邮件窗口中 我只想代码打开一个新的电子邮件窗口的内容。身体和细胞范围的详细信息(包含图表)。有人知道我的代码哪里出错了吗 Sub pasting01() Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.Cre

在类似页面上尝试了所有其他代码,但均失败

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

我只想代码打开一个新的电子邮件窗口的内容。身体和细胞范围的详细信息(包含图表)。有人知道我的代码哪里出错了吗

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

为了达到你的目的,你能胡乱处理以下事情吗

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
选项显式
子粘贴01()
Dim OutApp作为对象
将邮件变暗为对象
Set-OutApp=CreateObject(“Outlook.Application”)
Set-OutMail=OutApp.CreateItem(0)
将我的图表变暗为图表
设置myChart=ThisWorkbook.Worksheets(“Sheet1”).ChartObjects(“Chart 1”).Chart
将我的图片变暗为字符串
将文件名设置为字符串
将myPath设置为字符串
myPicture=“Chart1.png”
myPath=“C:\Users\User\Desktop\”
fileName=myPath&myPicture
导出文件名
发邮件
.TO=”xyz@anc.com"
.CC=”abc@xyz.com"
.Subject=“测试”
.Body=“亲爱的李先生”&vbNewLine
.Attachments.Add文件名
.HTMLBody=“第一行…

”&_ "" & _ “问候

”&_ “”和“更多文本”和“

” .展示 以 错误转到0 发送邮件=无 设置应用程序=无 终止文件名 端接头
结果:

如果您的代码有一些错误,请尝试在模块顶部使用选项Explicit


非常感谢代码与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