1

I'd like to set RTF formatted calendar entries, but don't know how to pass the byte[] to the ActiveX object, i.e. the RTFBody property.

The following code reads the RTFBody property after some content has been set - so reading the byte[] is working, but when I try to write exactly the same content (+ trailing 0) back, neither an U/Int8Array nor a Scripting.Directory works.

Maybe it's possible to workaround with some .NET objects, but I don't know how to instanciate those Non-ActiveX components. An alternative solution shouldn't require to script the formattings, e.g. "go to line 2 and make it bold", i.e. I like to generate the rtf via a template and only paste the result into the calendar object.

I'm aware that this has to be eventually encoded in Windows-1252, but for a start I simply want to see the same bytes to be written successfully. The script is executed within a HTA context - so script security is not an issue.

<html>
    <head>
        <hta:application id="foo" applicationname="foo" version="1" navigable="yes" sysMenu="yes"></hta>
    </head>
    <script language="JavaScript" type="text/javascript">
function doit2() {
    var rtfBody = 
        "{\\rtf1\\ansi\\ansicpg1252\\deff0\\nouicompat\\deflang1031{\\fonttbl{\\f0\\fswiss\\fcharset0 Calibri;}}\r\n"+
        "{\\*\\generator Riched20 14.0.7155.5000;}{\\*\\mmathPr\\mwrapIndent1440}\\viewkind4\\uc1\r\n"+
        "\\pard\\f0\\fs22 bla\\par\r\n"+
        "}\r\n";
    // https://github.com/mathiasbynens/windows-1252
    var rtfBody1252 = rtfBody; // windows1252.encode(rtfBody);

    var dict = new ActiveXObject("Scripting.Dictionary");

    for (var i = 0; i < rtfBody1252.length; i++) {
        dict.add(i, rtfBody1252.charCodeAt(i));
    }

    dict.add(rtfBody1252.length, 0);

    // Alternative setting via U/Int8Array also doesn't work ...
    // var buf = new ArrayBuffer(rtfBody1252.length+1);
    // var bufView = new Int8Array(buf);
    // for (var i=0, strLen=rtfBody1252.length; i<strLen; i++) {
    //  bufView[i] = rtfBody1252.charCodeAt(i);
    // }
    // bufView[rtfBody1252.length] = 0;

    var myOlApp = new ActiveXObject("Outlook.Application");
    var nameSpace = myOlApp.GetNameSpace("MAPI");
    var recipient = nameSpace.CreateRecipient("user@host.com");
    var cFolder = nameSpace.GetSharedDefaultFolder(recipient,9);

    var appointment = cFolder.Items.Add(1);
    appointment.Subject = "Subject";
    appointment.Location = "Location";
    appointment.Start = "22.02.2017 17:00";
    appointment.Duration = "120";
    appointment.Categories = "deleteme";
    appointment.Body = "bla";

    var va = new VBArray(appointment.RTFBody).toArray();
    var bla = String.fromCharCode.apply(null, va);
    document.forms[0].output.value = bla;
    // var bla2 = windows1252.decode(bla);

    appointment.RTFBody = dict.Items();
    appointment.ReminderSet = "true";
    appointment.Save();
    entryId = appointment.EntryId;
    appointment.Display();

    delete appointment;
    delete cFolder;
    delete recipient;
    delete nameSpace;
    delete myOlApp;
}
    </script>
    <body>
        <form>
            <input type="button" onclick="doit2()" value="doit"/>
            <textarea name="output" rows="5" cols="50"></textarea>
        </form>
    </body>
</html>
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
kiwiwings
  • 3,386
  • 1
  • 21
  • 57
  • A few more similar links on this topic: [SO issue](http://stackoverflow.com/questions/36086331) and the referenced [MSDN forum entry](https://social.msdn.microsoft.com/Forums/sqlserver/en-US/5dba0d12-94e8-47a5-b082-93d3693e2a47) – kiwiwings Feb 25 '17 at 01:48

0 Answers0