电子邮件身体丢失时发送(outlook vba) [英] E-Mail body is lost when sending (outlook vba)

查看:679
本文介绍了电子邮件身体丢失时发送(outlook vba)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



原始格式化的电子邮件(包括文本,表格和图片)的内容应该被复制并粘贴到一个新的电子邮件然后自动发送。一切正常,当我只显示消息,但实际发送电子邮件却不起作用。



这是我的代码:

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

'创建消息。
设置objMsg = Application.CreateItem(olMailItem)

'当前项的副本body
设置activeMailMessage = ActiveInspector.CurrentItem
activeMailMessage.GetInspector()。WordEditor.Range .FormattedText.Copy

'粘贴到新的电子邮件
Set BodyText = objMsg.GetInspector.WordEditor.Range
BodyText.Paste

'设置并发送通知电子邮件
与objMsg
.To =test@domain.com
.Subject =text
。发送
结束

文本应该粘贴到正文中,但不会粘贴:

 使用objMsg 
.To =test@domain.com
.Subject =test
.body = bodytext.paste
。发送
结束

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



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

解决方案

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



如果您直接设置HTMLBody(如果要保留格式),或在objMsg上的body属性然后它将像下面的例子一样工作。

 使用objMsg 
.HTMLBody = activeMailMessage.HTMLBody
.To =test@domain.com
.Subject =text
。发送
结束


另一个解决方案可能是使用MailItem的复制方法来创建您的新MailItem与原始项完全相同。这也将保留发送给您的电子邮件需要清除,以确保只有预期的收件人收到。

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

'创建新消息。
设置objMsg = Application.CreateItem(olMailItem)

'将当前项目分配给activeMailMessage
设置activeMailMessage = ActiveInspector.CurrentItem

'复制当前创建新消息的项目
设置objMsg = activeMailMessage.Copy

'清除电子邮件的所有现有收件人,因为这些将保留在复制
While objMsg中。收件人> 0
objMsg.Recipients.Remove 1
Wend

'设置并发送通知电子邮件
带objMsg
.To =test@domain.com
.Subject =text
。发送
结束

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


I'm trying to write a macro that sends an automatic notification to specific addresses before sending the original email. (Like a cc, without actually using cc.)

The content of the original formatted email, (including text, tables, and pictures,) should be copied and pasted into a new email which is then automatically sent. Everything works when I just display the message, but not when actually sending the email.

Here is my code:

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

The text should be pasted into the body like this, but it won't paste:

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

When I use .display the correct content is displayed. But when I send it directly (without first using .display), all of all information is lost and an empty email is sent. What can I do?

I could add a bcc in the original email to achieve the same result, but the original email does not always send, whereas this notification should be.

解决方案

Your code is never actually setting the Body of the e-mail in the objMsg object. It is working when you have objMsg displayed because your interacting with the 'Inspector'.

If you directly set either the HTMLBody (if you want to retain formatting), or the Body property on objMsg then it will work as in the below example.

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

Bob, regarding your question on images that are embedded within the e-mail being lost with the above approach. An alternate solution could be to use the MailItem's Copy method to create your new MailItem exactly as the original Item. This will also retain who the e-mail is being sent to you need to clear this to make sure only the intended recipients receive it.

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

This should retain your images and other attachments as they were in the original e-mail.

这篇关于电子邮件身体丢失时发送(outlook vba)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆