如何从电子邮件正文中的Excel发送形状对象?

时间:2015-08-30 18:17:49

标签: excel image vba email outlook

我正在努力尝试写一个宏中的这个(看似简单的)简单步骤。我有一个Excel形状对象,我正在尝试复制然后粘贴到Outlook邮件项目。形状对象实际上是使用Excel的“相机工具”的产物。我做了什么创建了一个图表,我需要翻转90度观看。相机工具使这很简单。我唯一的问题是现在将这个图片形状粘贴到outlook项目中。建议?

到目前为止,这是我的代码:

Sub emailer()

Dim wb As Workbook
Dim ws As Worksheet
Dim p As Shape
Dim o As Outlook.Application
Dim om As Outlook.MailItem
Dim rec As Outlook.Recipient
Dim recs As Outlook.Recipients

'create the outlook session
Set o = CreateObject("Outlook.Application")
'create the message
Set om = o.CreateItem(olMailItem)
Set recs = om.Recipients
Set rec = recs.Add("email@email.com")
rec.Type = 1

Set wb = ActiveWorkbook
Set ws = ActiveWorkbook.Worksheets("Chart_Pic")

Set p = ws.Shapes("Picture 2")
p.CopyPicture


With om
.SentOnBehalfOfName = "email@email.com"
.Subject = "Subject"
.HTMLBody = "This is a test" & vbCrLf & vbCrLf & [where I want the shape to go]


For Each rec In om.Recipients
rec.Resolve
Next

om.Send

End With

Set o = Nothing

1 个答案:

答案 0 :(得分:0)

您可以使用剪贴板复制/粘贴图片。但为此你需要一个可以处理剪贴板的Outlook邮件编辑器。以WordEditor为例。

示例:

Sub emailer()

 Set oOlApp = CreateObject("Outlook.Application")

 Set oOlMItem = oOlApp.CreateItem(olMailItem)

 Set oWB = ActiveWorkbook
 Set oWS = ActiveWorkbook.Worksheets("Chart_Pic")

 Set oPic = oWS.Shapes("Picture 2")
 oPic.CopyPicture ' oPic is now in Clipboard


 With oOlMItem

  .Display

  .To = "email@email.com"
  .Subject = "Subject"

  Set oOlInsp = .GetInspector
  Set oWdDoc = oOlInsp.WordEditor ' get Word Document from the MailBody

  Set oWdContent = oWdDoc.Content
  oWdContent.InsertParagraphBefore
  Set oWdRng = oWdDoc.Paragraphs(1).Range
  oWdRng.InsertBefore "This is a test"
  oWdRng.InsertParagraphAfter
  oWdRng.InsertParagraphAfter

  Set oWdRng = oWdDoc.Paragraphs(3).Range
  oWdRng.Paste ' paste from oPic Clipboard

  olFormatHTML = 2
  .BodyFormat = olFormatHTML ' change to HTML


 End With

End Sub