发送时电子邮件正文丢失(outlook vba)

时间:2015-04-20 13:31:51

标签: vba email outlook formatting

我尝试编写一个宏,在发送原始电子邮件之前向特定地址发送自动通知。 (就像一个cc,没有实际使用cc。)

应将原始格式化电子邮件(包括文本,表格和图片)的内容复制并粘贴到新电子邮件中,然后自动发送。当我只显示消息时,一切都有效,但实际发送电子邮件时则不然。

这是我的代码:

Dim objMsg As Outlook.MailItem
Dim activeMailMessage As Outlook.MailItem
Dim BodyText As Object

' Create the message.
Set objMsg = Application.CreateItem(olMailItem)

'copy body of current item
Set activeMailMessage = ActiveInspector.CurrentItem
activeMailMessage.GetInspector().WordEditor.Range.FormattedText.Copy

'paste body into new email
Set BodyText = objMsg.GetInspector.WordEditor.Range
BodyText.Paste

'set up and send notification email
With objMsg
    .To = "test@domain.com"
    .Subject = "text" 
    .Send
End With

文本应该像这样粘贴到正文中,但它不会粘贴:

With objMsg
    .To = "test@domain.com"
    .Subject = "test" 
    .body = bodytext.paste 
    .Send
End With

当我使用.display时,会显示正确的内容。但是当我直接发送它时(没有先使用.display),所有信息都会丢失并发送一封空电子邮件。我能做什么?

我可以在原始电子邮件中添加密件抄送以获得相同的结果,但原始电子邮件并不总是发送,而此通知应该是。

2 个答案:

答案 0 :(得分:2)

尝试在调用Paste方法后调用Save方法。

答案 1 :(得分:2)

您的代码实际上从未在objMsg对象中设置电子邮件的正文。当您显示objMsg时它正在工作,因为您与'Inspector'进行交互。

如果你直接设置HTMLBody(如果你想保留格式),或者objMsg上的Body属性,那么它将如下例所示。

With objMsg
    .HTMLBody = activeMailMessage.HTMLBody
    .To = "test@domain.com"
    .Subject = "text" 
    .Send
End With

Bob,关于您使用上述方法丢失电子邮件中嵌入的图像的问题。另一种解决方案可能是使用MailItem的Copy方法创建与原始Item完全相同的新MailItem。这也将保留发送给您的电子邮件的人需要清除它以确保只有目标收件人才能收到它。

Dim objMsg As Outlook.MailItem
Dim activeMailMessage As Outlook.MailItem

' Create the new message.
Set objMsg = Application.CreateItem(olMailItem)

' Assign the current item to activeMailMessage
Set activeMailMessage = ActiveInspector.CurrentItem

' Copy the current item to create a new message
Set objMsg = activeMailMessage.Copy

' Clear any existing recipients of the e-mail, as these will be retained in the Copy
While objMsg.Recipients.Count > 0
    objMsg.Recipients.Remove 1
Wend

'set up and send notification email
With objMsg
    .To = "test@domain.com"
    .Subject = "text" 
    .Send
End With

这应该保留原始电子邮件中的图像和其他附件。