将图表复制并粘贴到电子邮件VBA的正文中

时间:2018-12-21 17:07:41

标签: vba outlook outlook-vba

我正在尝试使用GetInspectorWordEditor将图表添加到电子邮件正文中。

我也很难同时向体内添加文本。根据我选择的段落位置,可以显示文本或显示图表,但不能同时显示两者。

代码示例:

Sub generateEmail()
Dim OutApp as Object
Dim OutMail as Object
Dim filePath as String
Dim cht as ChartObject
Dim vInspector as Object
Dim wEditor as Object

Set cht = wsData.ChartObjects("Chart 2")
cht.copy

With wsHome
filePath = ""
'also including an attachment which is working fine
End With

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

Set vInspector = OutMail.GetInspector
Set wEditor = vInspector.WordEditor

On Error Resume Next

With OutMail
.to = "All"
.CC = ""
.BCC = ""
.Subject = "Test"
.display
wEditor.Paragraphs(1).Range.Text = "Please see attached"
wEditor.Paragraphs(2).Range.Paste
'if I comment out paragraph 1 and change the second line to paragraph 1
'the chart prints perfectly, but the text does not show
'the way its set up now, only the "Please see attached" shows up

.Attachments.Add (filePath)
End With

Set OutMail = Nothing
Set OutApp = Nothing

End Sub

在使用wordEditor读取段落的方式上,我肯定会丢失一些东西,但没有足够有效地进行故障排除。

1 个答案:

答案 0 :(得分:1)

On Error Resume Next 隐藏正在发生的错误。删除它,您会得到

  

运行时错误'5941':所请求的集合成员不存在。

电子邮件正文只有1个段落;您不能将其粘贴到第2段中,因为它不存在。也许Add第二段,然后粘贴:

wEditor.Paragraphs(1).Range.Text = "Please see attached"
wEditor.Paragraphs.Add
wEditor.Paragraphs(2).Range.Paste