1

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.

Bob
  • 13
  • 7
  • Does it send fine if you click send from outlook once you show it? Or does it still miss off information? – Sam Apr 20 '15 at 15:24
  • @sam: yes, that also works. also runs into the same problem as with andshrew 's solution: the picture which is part of the message body falls out. – Bob Apr 21 '15 at 07:03

2 Answers2

3

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.

andshrew
  • 418
  • 4
  • 8
  • It doesn't matter whether an inspector window is displayed or not. The code should work correctly. See [Chapter 17: Working with Item Bodies](https://msdn.microsoft.com/en-us/library/dd492012%28v=office.12%29.aspx?f=255&MSPPError=-2147217396#Outlook2007ProgrammingCh17_UsingWordEditor). – Eugene Astafiev Apr 20 '15 at 16:01
  • @EugeneAstafiev Reading your link I'm not entirely sure that it _should_ work when the Inspector isn't displayed - when I tried the code it didn't work unless it was displayed. But regardless, it seems redundant to be copying the body like this when you have access to the original MailItem, and your own MailItem. Simply assigning the HTMLBody directly seems to make most sense. – andshrew Apr 20 '15 at 16:41
  • @andshrew, what you recommended works. now i run into another small issue though: usually there is a screenshot included into the email. when using the code like this, it says "the linked image cannot be displayed. the file may have been moved, renamed....." what can i do about that? as far as i understood it so far, I'm copying the entire body and pasting it in a new message (much like forwarding the original message). in the latter case pictures are copied without any issues though, whereas my case is unsuccessful... – Bob Apr 21 '15 at 06:42
  • @Bob, the issue here will be that your images are stored as Attachments in the MailItem and they aren't being copied when we set the Body or HTMLBody. I've offered an alternate solution which would resolve this, I've added it to the answer as there isn't enough space here. – andshrew Apr 21 '15 at 10:38
  • @andshrew, tried this too, but didn't work. The problem is that it does forward the message (that is, the subject is exactly the same as the original message (without the FW though)), but the Body is completely empty again. also when I add the `.HTMLBody = activeMailMessage.HTMLBody` the body remains empty. Guess that for some reason it thus does not forward it, but I wonder what it does... – Bob Apr 21 '15 at 12:36
  • might it have to do with the fact that I'm using outlook 2010 without the MAPI thing I keep reading about on forums? FYI I do have the reference Library activated. – Bob Apr 21 '15 at 13:00
  • @Bob Sorry I misread your original question and realise you're doing this with an un-sent e-mail so the Forward suggestion doesn't work. I've amended the solution, instead using the Copy function to create a copy of the MailItem. This also retains the recipients so you need to clear this before adding your new recipient. I've tested in Outlook 2010 and appears to be all OK so I'm hopeful it works for you too. – andshrew Apr 21 '15 at 15:53
  • @andshrew, Great, that works :-D it's doing everything i planned now! also, I would have voted for your answer being useful, but I don't have a high enough reputation yet to do so, so I hope my sincere Thanks here will do the trick ;-) – Bob Apr 22 '15 at 07:01
2

Try to call the Save method after calling the Paste method.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • 'paste body into new email Set BodyText = objMsg.GetInspector.WordEditor.Range BodyText.Paste objmsg.save 'set up and send notification email With objMsg .To = "test@domain.com" .Subject = "text" .Send End With which didn't work either... – Bob Apr 20 '15 at 14:23
  • Did you try to check out the Body or HTMLBody property values after calling the Save method? – Eugene Astafiev Apr 20 '15 at 15:30
  • Try to use the Display() and Save()methods then. Does it help? – Eugene Astafiev Apr 20 '15 at 16:02
  • to be honest i wouldn't even know how to do that. your link in the first reply was very helpful as I'm quite the newby with programming vba.... – Bob Apr 21 '15 at 06:56