2016-06-28 4 views
1

Ich habe ein .NET MVC-Projekt für Word online. Das Add-In wird erfolgreich gestartet, aber es lädt nicht "Home.js", das Office.initialize aufruft, um Daten in den Wortkörper einzufügen. HierOffice.js Funktion funktioniert nicht in MVC-Anwendung für Word Online

ist _Layout.cshtml:

<html> 
<head> 
    <meta charset="utf-8" /> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <title></title> 
    @Styles.Render("~/bundles/css") 
    @Scripts.Render("~/bundles/modernizr") 
</head> 
<body ng-app=""> 
    <div class="container"> 
     @RenderBody() 
    </div> 
    @Scripts.Render("~/bundles/jquery") 
    @Scripts.Render("~/Scripts/Office/1/office.js") 
    @Scripts.Render("~/Scripts/Home.js") 
    @Scripts.Render("~/bundles/bootstrap") 
    @Scripts.Render("~/Scripts/angular.min.js") 
</body> 
</html> 

Und Home.js:

(function() { 
    "use strict"; 

    Office.initialize = function (reason) { 
     $(document).ready(function() { 
      if (!Office.context.requirements.isSetSupported('WordApi', '1.1')) { 
       return; 
      } 
      loadSampleData(); 
     }); 
    }; 

    function loadSampleData() { 

     // Run a batch operation against the Word object model. 
     Word.run(function (context) { 
      // Create a proxy object for the document body. 
      var body = context.document.body; 
      // Queue a commmand to clear the contents of the body. 
      body.clear(); 
      // Queue a command to insert text into the end of the Word document body. 
      body.insertText("This is a sample text inserted in the document", 
          Word.InsertLocation.end); 

      // Synchronize the document state by executing the queued commands, and return a promise to indicate task completion. 
      return context.sync(); 
     }) 
     .catch(errorHandler); 
    } 

    function errorHandler(error) { 
     console.log("Error: " + error); 
     if (error instanceof OfficeExtension.Error) { 
      console.log("Debug info: " + JSON.stringify(error.debugInfo)); 
     } 
    } 
})(); 

Danke.

+0

Ich möchte auch das eckige Bootstrapping-Problem kommentieren. Ich habe versucht, Winkel der Anwendung vollständig herauszunehmen, und das Problem besteht weiter. – michaelmilone

+0

Wird home.js überhaupt geladen? Wenn Sie zum Beispiel eine console.log an die Spitze stellen, wird es gedruckt? Wenn ja, wenn Sie eine console.log als erste Zeile in Office.initialze einfügen, wird es gedruckt? –

+0

danke für die Antwort. Ja, es druckt. – michaelmilone

Antwort

0

Sie führen nur aus, wenn WordAPI v1.1 verfügbar ist. Diese API wird nur in Word 2016 for Windows, Mac and iPad unterstützt. Wenn Sie Word Online oder Word 2013 verwenden, wird dieser Code beendet (return), ohne etwas auszuführen.

Eine andere Anmerkung, Sie beziehen sich auf Office.js und Home.js am Ende der <body>. Sie haben nur 5 Sekunden Zeit, um Office.initialize() fertig zu stellen. Wenn Sie es am unteren Rand der Seite platzieren, muss alles geladen werden, bevor der Browser diese Funktion ausführt. Während dies für eine Website absolut richtig ist, führt dies zu Timeout-Fehlern bei Add-Ins. Diese Referenzen should always be placed within the <header>.

+0

sehr gut, danke marc! – michaelmilone

Verwandte Themen