Kapil Sharma
Kapil Sharma

Reputation: 148

Header and footer is not binding in word javascript api

I am working on a Word Add-in for one or our applications. Using this Word add-in, send saved Word templates from our application to Word.

If I use a template that is includes a header and footer, these do not show up in the Word document.

Here is the code:

function setDocumentDataBase64(data) {
    Word.run(function (context) {
            // Create a proxy object for the document body.
            var body = context.document.body;
            //cleaning old context
            //body.clear();
            body.insertFileFromBase64(data, Word.InsertLocation.replace);
            return context.sync().then(function () {
                alert.success("Document inserted");
            });
        })
        .catch(function (error) {
            console.log('Error: ' + JSON.stringify(error));
            if (error instanceof OfficeExtension.Error) {
                console.log('Debug info: ' + JSON.stringify(error.debugInfo));
            }
        });
}

Upvotes: 2

Views: 960

Answers (1)

Juan Balmori
Juan Balmori

Reputation: 5036

I am not sure what you mean by "binding" on this question (as "binding" is a very specific concept in Office.js). I am guessing that what you are trying to do is to insert a document containing header/footers via range.insertFileFromBase64 and you are not seeing the header and footers after the insertion, if that's the case its a by design issue, and the reason, we don't want to replace the header/footer of the current document. The goal of the method is to reuse chunks of documents, not replacing the entire document.

If you need to change the header, you need to do so manually.

You can explore the createDocument API (who actually opens a new document window) is in preview and might be what you need.

Hope this helps. thanks! Juan.

this is the sample code of getting the base64 of the current document:

 function getFile(){
        Office.context.document.getFileAsync(Office.FileType.Compressed, { sliceSize: 4194304  /*64 KB*/ },
          function (result) {
              if (result.status == "succeeded") {
                  // If the getFileAsync call succeeded, then
                  // result.value will return a valid File Object.
                  var myFile = result.value;
                  var sliceCount = myFile.sliceCount;
                  var slicesReceived = 0, gotAllSlices = true, docdataSlices = [];
                  console.log("File size:" + myFile.size + " #Slices: " + sliceCount);

                  // Get the file slices.
                  getSliceAsync(myFile, 0, sliceCount, gotAllSlices, docdataSlices, slicesReceived);
              }
              else {
                  app.showNotification("Error:", result.error.message);
              }
          });

    
    
    }


    function getSliceAsync(file, nextSlice, sliceCount, gotAllSlices, docdataSlices, slicesReceived) {
        file.getSliceAsync(nextSlice, function (sliceResult) {
            if (sliceResult.status == "succeeded") {
                if (!gotAllSlices) { // Failed to get all slices, no need to continue.
                    return;
                }

                // Got one slice, store it in a temporary array.
                // (Or you can do something else, such as
                // send it to a third-party server.)
                docdataSlices[sliceResult.value.index] = sliceResult.value.data;
                if (++slicesReceived == sliceCount) {
                    // All slices have been received.
                    file.closeAsync();
                    onGotAllSlices(docdataSlices);
                }
                else {
                    getSliceAsync(file, ++nextSlice, sliceCount, gotAllSlices, docdataSlices, slicesReceived);
                }
            }
            else {
                gotAllSlices = false;
                file.closeAsync();
                console.log("getSliceAsync Error:", sliceResult.error.message);
            }
        });
    }

    function onGotAllSlices(docdataSlices) {
        var docdata = [];
        for (var i = 0; i < docdataSlices.length; i++) {
            docdata = docdata.concat(docdataSlices[i]);
        }
        var fileContent = new String();

        for (var j = 0; j < docdata.length; j++) {
            fileContent += String.fromCharCode(docdata[j]);
        }
       
 


        var mybase64 = window.btoa(fileContent);
        console.log("here is the base 64", mybase64);
        // Now all the file content is stored in 'fileContent' variable,
        // you can do something with it, such as print, fax...
    }

Upvotes: 2

Related Questions