E-mail body is lost when sending (Outlook vba)

I am trying to write a macro that sends automatic notification to specific addresses before the original email is sent. (Like a cube, 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 show the message, but not when I send it by 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 inserted into the body this way, but it won't be inserted:

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

      

When I use the .display

correct content is displayed. But when I send it directly (no use .display

) all information is lost and a blank email is sent. What can I do?

I could add the BCC to the original email to achieve the same result, but the original email is not always sent, whereas this notification should be.

+1


source to share


2 answers


Your code never sets the body of the email message in the objMsg object. It works when you show objMsg, because you are interacting with the Inspector.

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

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

      



Bob, in regards to your question about images that are embedded in an email message lost with the above approach. An alternative solution would be to use the MailItem Copy method to create a new MailItem just like the original item. This will also save who the email is being sent to, you need to clean it up to make sure only the intended recipients are receiving 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 keep your images and other attachments as they were in the original email.

+2


source


Try calling the Save method after calling the Insert method.



+2


source







All Articles