在Word2016

时间:2016-02-04 21:44:35

标签: office-js word-addins

我正在为Word编写一个加载项。我有Word 2016. Office.context.requirements.isSetSupported('WordApi', 1.1)在这种情况下应该返回true,但它返回false。并且Word对象未定义。有任何想法吗?谢谢。

Windows 7企业版上的Microsoft Word 2016 MSO(16.0.6326.1022)32位

以下是我的代码的一些片段:

在我head的{​​{1}}我有这个:

html

common.js以

开头
<script src="https://appsforoffice.microsoft.com/lib/1/hosted/Office.js" type="text/javascript"></script>
<script src="local/js/common.js"></script>

Office.initialize = function(reason) { console.log(Office.context.requirememts) if (Office.context.requirements.isSetSupported('WordApi', 1.1)) { //never gets in here } else { console.log("This add-in requires Word 2016 or greater."); } }; 没有Office.context.requirememts功能

更新: 有一件事我没想到是相关的,但显然它是。我加载的html页面重定向到另一个html页面,这似乎导致了问题。我有重定向的原因是因为当xml文件有

isSetSupported

而不是

<SourceLocation DefaultValue="http://localhost/wordaddin/index.html"/>

高速缓存html文件的内容,并且没有办法(至少我没有找到)清除此缓存。因此,无论我在index.html中所做的任何更改都无法实现。因此,在index.html的负载下,我做了<SourceLocation DefaultValue="C:\WordAddIn\index.html"/>并且这样做了,但后来我陷入了这种奇怪的境地。

这是index.html:

window.location='main.html?'+datestamp

4 个答案:

答案 0 :(得分:0)

页面上有typedef struct { int id; char *string; } entry_t; char *str = "small string"; entry_t *entry = malloc (sizeof (*entry) + strlen (str) + 1); // with extra bytes. entry->id = 7; entry->string = (char*)entry + sizeof (entry_t); strcpy (entry->string, str); 吗?您提到的API仅在Office有机会初始化后才可用...

您看到的所有样本都应包含

Office.initialize

线。

~Michael Zlatkovsky,Office Extensibility团队的开发人员,MSFT

答案 1 :(得分:0)

对我来说唯一有用的东西(我必须说这是一个可怕的解决方案)是在我的xml中将html文件的地址添加一个查询字符串参数

<SourceLocation DefaultValue="http://localhost/wordaddin/index.html?1"/>

然后我需要在每次更改index.html时更改此参数并重新附加加载项。

如果有更好的解决方案,请分享。

答案 2 :(得分:0)

这里有许多信息缺失,所以我会尽力清除我的所有假设。 有一点可以肯定的是,您不需要更改清单(它在此问题上引用的内容为&#34; xml文件&#34;)以启用您的方案。

一个。我看到你在胜利7 湾您正在使用我们基于文件共享的目录,该目录已正确配置为受信任位置。 C。您在该位置删除了清单(xml)。 d。清单的源位置指向正确的html位置。

如果上述所有假设都是正确的,那么您应该没有任何问题。 我把html和js文件放在同一个文件夹上。

这是Html文件的内容

    <!DOCTYPE html>
    <html>
    <head>

   <script src="https://appsforoffice.microsoft.com/lib/1/hosted/Office.js" type="text/javascript"></script>
   <script src="common.js"></script>

<title></title>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<script>
    function init() {
        var timestamp = new Date().valueOf();
    //    window.location = "main.html?" + timestamp;
    }
</script>
</head>
<body onload="init()"> 
      Hello World!
<div id='result'></div>
</body>
</html>

请注意,我评论了window.location东西,因为这不需要。 我添加了一个div来显示需求验证的结果。

这是js文件的内容

Office.initialize = function(reason) {
console.log(Office.context.requirememts)
if (Office.context.requirements.isSetSupported('WordApi', 1.1)) {
    //never gets in here
 document.getElementById("result").innerText= "Requirement supported.";
  }
  else {
    document.getElementById("result").innerText= "This add-in requires Word 2016 or greater.";
   }
  };

最后这是清单:

 <?xml version="1.0" encoding="UTF-8"?>
 <!--Created:cb85b80c-f585-40ff-8bfc-12ff4d0e34a9-->
 <OfficeApp xmlns="http://schemas.microsoft.com/office/appforoffice/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="TaskPaneApp">
 <Id>90400971-e367-4e1b-b9bf-fc2163635c37</Id>
 <Version>1.0.0.0</Version>
 <ProviderName>Juanelo Balmori</ProviderName>
<DefaultLocale>en-US</DefaultLocale>
<DisplayName DefaultValue="Code FLow Word.js" />
<Description DefaultValue="Code Flow Sample!"/>

<Capabilities>
   <Capability Name="Document" />
 </Capabilities>
<DefaultSettings>
<SourceLocation DefaultValue="\\*name of your share*\index.html" />
</DefaultSettings>
<Permissions>ReadWriteDocument</Permissions>
</OfficeApp>

请注意,您需要为基于文件共享的目录信任位置创建共享。

如果所有这些都已到位,我将获得预期的功能。如果我改变了HTML文件的内容,那么当我F5任务窗格时会反映出来。

请注意,你必须得到一个栏来启用运行本地html页面,只需确保在任务窗格中允许它,你应该没问题。

谢谢!

答案 3 :(得分:0)

我有同样的问题。使用协议https可能有所帮助。

第一次加载时,http或https工作正常。如果您从其他某个站点回来,则必须使用https。