Question

I am pretty new to JavaScript / jQuery and hope someone here can help me with the following:

I am using the following VBA code in an Excel file in order to prepare a formatted email in Outlook. The code is short, easy to adjust and works perfect, incl. preparing the email recipient, the subject and the HTML body.

As this works without issues in VBA I hope there is also a way to achieve the same in JavaScript / jQuery. I tried mailto. This works for the recipient and subject but not for the HTML body which is displayed as plain text instead.

My VBA code (working):

Sub Test()
Dim olApp As Object
Dim olOldBody As String

Set olApp = CreateObject("Outlook.Application")

With olApp.CreateItem(0)
    .GetInspector.Display
    olOldBody = .HTMLBody
    .Importance = 2
    .To = "email@company.com"
    .Cc = ""
    .Bcc = ""
    .Subject = "My subject text"
    .HTMLBody = "<span style='font-family: Arial, Helvetica, sans-serif; font-size:11pt; color: #00457C;'>" & _
        "<strong>Title1:</strong>Line1<br />" & _
        "<strong>Title2:</strong>Line2<br />" & _
        "<strong>Title3:</strong>Line3<br /></span>" & olOldBody
    .Display
End With

End Sub

Thanks for any help with this, Tim

Was it helpful?

Solution

Outlook Object Model is COM based, which means it can only be used from IE.

Even then, your script must come from a trusted site to be able to create local COM objects using new ActiveXObject().

Updated: try JS code like the following (off the top of my head, I am not a Java programmer).
Note that in general you cannot concatenate two HTML strings and produce a valid HTML string. You need to merge the two (look for the position of the "", insert your new HTML at that position).

var olApp = new ActiveAxObject("Outlook.Application");
    var olItem = olApp.CreateItem(0);
    var olOldBody = olItem.HTMLBody;
    olOldBody.Importance = 2;
    olItem.To = "email@company.com";
    olItem.Cc = "";
    olItem.Bcc = "";
    olItem.Subject = "My subject text";
    olItem.HTMLBody = "<span style='font-family: Arial, Helvetica, sans-serif; font-size:11pt; color: #00457C;'>" +
        "<strong>Title1:</strong>Line1<br />" +
        "<strong>Title2:</strong>Line2<br />" +
        "<strong>Title3:</strong>Line3<br /></span>" + olOldBody;
    olItem.Display();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top