Vba将Excel范围转换为图片并发送到Outlook,将文本写入正文

时间:2017-03-14 13:42:37

标签: excel image vba outlook range

我想从受保护的Excel工作表复制一个范围,并将其作为图片粘贴到Outlook中,写一些文本并显示它。

我的代码首先粘贴文本,然后粘贴图片,但同时删除文本。但是我想要文本和文本下的图片(范围从Excel转换成图片)。

任何人都可以帮助我如何获得它吗?

Sub Send_Email()

   Dim r As Range
   Set r = Range("NR7:OD39")

   Dim outlookApp As Outlook.Application
   Set outlookApp = CreateObject("Outlook.Application")

   Dim OutMail As Outlook.MailItem
   Set OutMail = outlookApp.CreateItem(olMailItem)

   Dim StrFileName As String

   Application.DisplayAlerts = False
   Application.ScreenUpdating = False

   Sheets("table1").Select
   ActiveSheet.Unprotect Password:="blabla"

   ActiveSheet.Outline.ShowLevels RowLevels:=8, ColumnLevels:=8

   r.Select
   r.Copy

   OutMail.Display

   Dim Email As Word.Document
   Set Email = OutMail.GetInspector.WordEditor

   With OutMail

      .To = "Name.surname@amazon.com"
      .CC = "Surname.Name@amazon.com"
      .Subject = "Subject"
      .Body = "Hi everybody," & vbNewLine & "actual Status"
      .Display

   End With

   Email.Range.PasteAndFormat wdChartPicture

   ActiveSheet.Outline.ShowLevels RowLevels:=1, ColumnLevels:=1
   ActiveSheet.Protect Password:="blabla"

End Sub

1 个答案:

答案 0 :(得分:1)

从这一行开始

Set Email = OutMail.GetInspector.WordEditor

这应该这样做:

Dim ran as Word.Range
    Set Email = OutMail.GetInspector.WordEditor

       With OutMail

          .To = "Name.surname@amazon.com"
          .cc = "Surname.Name@amazon.com"
          .Subject = "Subject"
          .Body = "Hi everybody," & vbNewLine & "actual Status"
          .Display

       End With
    Email.Range.InsertAfter vbCrLf
    Set ran = Email.Range(Email.Content.End - 1, Email.Content.End - 1)
    ran.PasteAndFormat wdChartPicture