如何从Javascript设置ActiveX组件的byte []属性?
我想设置RTF格式的日历条目,但不知道如何将字节[]传递给ActiveX对象,即RTFBody
属性。如何从Javascript设置ActiveX组件的byte []属性?
下面的代码读取RTFBody
属性后,一些内容已定 - 所以读完的byte []的工作,但是当我试图准确地写相同的内容(+尾随0)回来了,既不是U/Int8Array
也不是Scripting.Directory
作品。
也许有可能解决一些.NET objects,但我不知道如何instanciate这些非ActiveX组件。另一种解决方案不需要脚本化格式,例如“转到第2行并将其设为粗体”,即我喜欢通过模板生成rtf,并仅将结果粘贴到日历对象中。
我知道这必须最终编码在Windows-1252
,但一开始我只想看到相同的字节写入成功。该脚本在HTA上下文中执行 - 因此脚本安全性不是问题。
<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("[email protected]");
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>
有一个已知问题MailItem.RtfBody
财产使用后期绑定被设置(这就是JS使用 - IDispatch.GetIDsOfNames/Invoke
)。早期投标(通过v表调用)工作正常。
上次我听说这个,没有解决方法,至少在你使用JS时没有。你可以尝试使用Redemption(它暴露了它的安全*项目对象和RDOMail对象RtfBody属性),但随后你需要进入你的脚本将运行的每台机器上安装它..
感谢您指出这一点 - 在寻找解决方案时,我通过一个绑定解释来解决问题,但并不理解它。现在使用新的搜索表达式,我发现[您的帖子](https://social.msdn.microsoft.com/Forums/vstudio/en-US/3c02ffe9-7375-4fed-804f-514820b6fe57) - 遗憾地提供了Redemption在这里不是一个选项。也许提到的扩展MAPI有任何帮助...但可能我会留下未格式化的内容... – kiwiwings
...正如你在[网站](http://www.dimastr.com/redemption/ home.htm)...“赎回使用扩展MAPI(不受安全修补程序影响,因为脚本语言无法访问)”:(:) – kiwiwings
是的,JS无法执行扩展MAPI,即使C#也无法使用它(在至少不容易), –
多几个类似的链接本主题:[SO问题](http://stackoverflow.com/questions/36086331)和引用的[MSDN论坛条目](https://social.msdn.microsoft.com/Forums/sqlserver/en-US/5dba0d12 -94e8-47a5-b082-93d3693e2a47) – kiwiwings