首页 > 解决方案 > 使用自己的浏览器控件扩展 Office JavaScript API

问题描述

我正在尝试使用 System.Windows.Forms.WebBrowser-Control 编写一个 VSTO-Add-In,以启用类似于 Office-JS-Add-In 模型的功能。

WebBrowser 控件将显示一些 HTML/JS 页面,并能够通过 window.external 和 WebBrowser 对象的 ObjectForScripting 属性从 JavaScript 调用 VSTO-Add-In 中的 C# 函数。

那就是在JS中调用将是

window.external.DoFancyStuffToMyDocument(withTheseParams)

虽然必须有一些

class MyFunctionProxy() {
    public void DoFancyStuffToMyDocument(string theParam) {
        //code here
    }
}

在 C#-Code 中,这将附加到 WebBrowser

myWebBrowser.ObjectForScripting = new MyFunctionProxy();

到目前为止,一切都很好。现在来了。我希望我的 HTML/JS 代码也能够使用 office.js 代码和功能,例如

Word.run(function (context) {
    var thisDocument = context.document;
    var range = thisDocument.getSelection();
    range.insertText('"Hitch your wagon to a star."\n', Word.InsertLocation.replace);
    //...
}

有没有人看到让这个工作的方法?

我最初的猜测是,Word on-prem 中的 OfficeJS-taskpane-add-ins 使用了一些与上面类似的方法,以及从 WebBrowser 和适当的 ObjectForScripting 派生的类。这表明必须有一个(希望可访问的)类分配给处理来自 office.js 的函数调用的 ObjectForScripting-property。然后我可以代理这个 ObjectForScripting 类并添加我自己的函数,如“DoFancyStuffToMyDocument()”。

标签: vstowebbrowser-controloffice-js

解决方案


推荐阅读