0
votes

I need to programatically open outlook 2016 on users computer, with new message, which contains the predefined fields (To, Bcc, UTF-8 body, attachment). For that, I need to generate either a file which outlook opens as a new message, or a script which makes outlook open the new message.

It may look like an easy task, but it is actually tricky. I would, for example, do it in a way that I generate an .eml file, with content like this:

From: [email protected]
To: [email protected]
Cc: [email protected]
Bcc: [email protected]
X-Unsent: 1
Subject: Something

This is a test message.
Multipart can be used to add attachment.

The problem is that this won't work, because if such file is opened by outlook (as .eml file), outlook is able to open it, but it ignores Bcc line entirely.

So in another iteration, I would try to make a VBS script instead:

Set objoutlookApp = CreateObject("Outlook.Application") 
Set objmessage = objoutlookApp.CreateItem(olMailItem) 
objmessage.TO = "[email protected];[email protected]"
objmessage.CC = "[email protected];[email protected]"
objmessage.BCC = "[email protected]"
objmessage.Subject = "E-Mail Subject"
objmessage.Body = "Here comes some text"
objmessage.display
set objmessage = Nothing
set objoutlookApp = Nothing
wscript.quit

This seems a bit better, but is still insufficient. First of all, the VBS file cannot be in UTF-8 format, thus it's not possible to send an email in chinese, for example, I need to be able to write UTF-8 encoded string directly to the body since it needs to be a single-file solution. And second, I have no idea how to add atachments (multipart) this way.

Is there any way to open new message window in outlook with predefined fields (including Bcc), by a file which I can generate server side and then send to the user to open?

2
How about loading the UTF-8 body using ADODB: wiki.mcneel.com/developer/scriptsamples/readutf8Alastair McCormack

2 Answers

0
votes

The Outlook object model provides three main ways for working with item bodies:

  1. Body - a plain text without any mnarkup.
  2. HTMLBody - a HTML markup which represents the message body.
  3. The Word editor. The WordEditor property of the Inspector class returns an instance of the Word Document which represents the message body. So, you can use the Word object model do whatever you need with the message body.

It is up to you which way is to choose. See Chapter 17: Working with Item Bodies for more information.

You can use the Add method of the Attachments class which reates a new attachment in the Attachments collection. For example:

 Sub AddAttachment() 
  Dim myItem As Outlook.MailItem 
  Dim myAttachments As Outlook.Attachments 

  Set myItem = Application.CreateItem(olMailItem) 
  Set myAttachments = myItem.Attachments 
  myAttachments.Add "D:\Documents\Q496.xlsx", _ 
    olByValue, 1, "4th Quarter 1996 Results Chart" 
  myItem.Display 
 End Sub

Finally, you may find the How to automate Outlook from another program article helpful.

0
votes

Your VBS file cannot be UTF-8 encoded, but VB script works just fine with UTF-16 encoded files. I had no problem with the following file saved in UTF-16 encoding from Notepad:

Set objoutlookApp = CreateObject("Outlook.Application") 
Set objmessage = objoutlookApp.CreateItem(olMailItem) 
objmessage.TO = "[email protected];[email protected]"
objmessage.CC = "[email protected];[email protected]"
objmessage.BCC = "有些BCC名 <[email protected]>"
objmessage.Subject = "E-Mail Subject"
objmessage.Body = "Here comes some text"
objmessage.display
set objmessage = Nothing
set objoutlookApp = Nothing