且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将Excel工作表中的文本和图表复制到Outlook正文?

更新时间:2022-12-03 13:21:40

也许是这样的:

Sub createJpg(Namesheet As String, nameRange As String, nameFile As String)
    ThisWorkbook.Activate
    Worksheets(Namesheet).Activate
    Set Plage = ThisWorkbook.Worksheets(Namesheet).Range(nameRange)
    Plage.CopyPicture
    With ThisWorkbook.Worksheets(Namesheet).ChartObjects.Add(Plage.Left, Plage.Top, Plage.Width, Plage.Height)
        .Activate
        .Chart.Paste
        .Chart.Export Environ$("temp") & "\" & nameFile & ".jpg", "JPG"
    End With
    Worksheets(Namesheet).ChartObjects(Worksheets(Namesheet).ChartObjects.Count).Delete
Set Plage = Nothing
End Sub

并在您现有的代码中:

Set appOutlook = CreateObject("outlook.application")
'create a new message
Set Message = appOutlook.CreateItem(olMailItem)
With Message
    .HTMLBody = "Hello" ' and whatever else you need in the text body
    'first we create the image as a JPG file
    Call createJpg("Dashboard", "B8:H9", "DashboardFile")
    'we attached the embedded image with a Position at 0 (makes the attachment hidden)
    TempFilePath = Environ$("temp") & "\"
    .Attachments.Add TempFilePath & "DashboardFile.jpg", olByValue, 0

    'Then we add an html <img src=''> link to this image
    'Note than you can customize width and height - not mandatory

    .HTMLBody = .HTMLBody & "<br><B>WEEKLY REPPORT:</B><br>" _
        & "<img src='cid:DashboardFile.jpg'" & "width='814' height='33'><br>" _
        & "<br>Best Regards,<br>Ed</font></span>"

    .To = "contact1@email.com; contact2@email.com"
    .Cc = "contact3@email.com"

    .Display
    '.Send
End With