如何在电子邮件正文中发送来自 Excel 的形状对象?
How to send a shape object from Excel in the body of an Email?
我正在尝试编写宏中的这一(看似)简单的步骤。我有一个 Excel 形状对象,我正在尝试将其复制然后粘贴到 Outlook 邮件项目中。形状对象实际上是使用 Excel 的 "camera tool." 的产物 我所做的是创建一个图表,我需要翻转 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
您可以将剪贴板用于 copy/paste 图片。但是为此,您需要一个可以处理剪贴板的 Outlook 邮件编辑器。以文字编辑器为例。
示例:
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
我正在尝试编写宏中的这一(看似)简单的步骤。我有一个 Excel 形状对象,我正在尝试将其复制然后粘贴到 Outlook 邮件项目中。形状对象实际上是使用 Excel 的 "camera tool." 的产物 我所做的是创建一个图表,我需要翻转 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
您可以将剪贴板用于 copy/paste 图片。但是为此,您需要一个可以处理剪贴板的 Outlook 邮件编辑器。以文字编辑器为例。
示例:
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