如何从Javascript设置ActiveX组件的byte []属性?

时间:2017-02-24 15:40:28

标签: javascript activex outlook-vba typeconverter

我想设置RTF格式的日历条目,但不知道如何将byte []传递给ActiveX对象,即RTFBody属性。

以下代码在设置了一些内容后读取RTFBody属性 - 因此读取byte []是有效的,但是当我尝试写回完全相同的内容(+尾随0)时,两者都没有{ {3}}也不是U/Int8Array

也许可以与某些Scripting.Directory一起解决,但我不知道如何实现这些非ActiveX组件。替代解决方案不应该要求格式化脚本,例如"转到第2行并使其变为粗体",即我想通过模板生成rtf并仅将结果粘贴到日历对象中。

我知道这最终必须在Windows-1252中编码,但是一开始我只想看到要成功写入的相同字节。该脚本在.NET objects上下文中执行 - 因此脚本安全性不是问题。

<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>

1 个答案:

答案 0 :(得分:1)

使用后期绑定设置MailItem.RtfBody属性存在一个已知问题(这是JS使用的 - IDispatch.GetIDsOfNames / Invoke)。早期招标(通过v-table调用)工作正常。

上次我听说过这个,没有解决方法,至少在你使用JS时没有。您可以尝试使用Redemption(它在其Safe * Item对象和RDOMail对象上公开RtfBody属性),但是您需要在运行脚本的每台机器上安装它。