Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

vba - Pasting Chart into Outlook Email from Excel

Tried all other codes on similar pages but failed to work.

This is my current version. Works only if I currently have a new email window open and oddly, my code will paste the .body and cell range details into 2 separate new email windows.

I just want the code to open a new email window with contents .body and cell range details (contains chart). Anybody have any ideas where my code went wrong?

Sub pasting01()

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

With OutMail
.TO = "[email protected]"
.CC = "[email protected]"
.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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have some errors on your code, try using Option Explicit top of your module

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 = "[email protected]"
        .CC = "[email protected]"
        .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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...