Sk Asif
Sk Asif

Reputation: 75

insert doc file into the current oppened document at cursor point (using insertFileFromBase64 )

Here I am trying to insert a doc file at cursor point from my Office Add-in to Word , but I could not find the proper solution. The API has only three options :

bodyObject.insertFileFromBase64(base64File, insertLocation);

where insertLocation can be Start, End or Replace.

Upvotes: 2

Views: 320

Answers (1)

Marc LaFleur
Marc LaFleur

Reputation: 33094

The options for Word.InsertLocation are:

  • Start: Prepend the inserted content before the existing content.
  • End: Append the inserted content after the existing contents.
  • Replace: Replace the existing content with the inserted content.

When you using bodyObject.insertFileFromBase64, you're scoping your call to the entire Body of the document. Calling this method will therefore not care about your cursor location.

I suspect that you really want here is rangeObject.insertFileFromBase64. This is scoped to a Range rather than the entire Body. You can fetch a range from the current selection (or if nothing is selected, your cursor location):

Word.run(function (context) {

    // Queue a command to get the current selection and then
    // create a proxy range object with the results.
    var range = context.document.getSelection();

    // Queue a commmand to insert base64 encoded .docx at the beginning of the range.
    // You'll need to implement getBase64() to make this work.
    range.insertFileFromBase64(getBase64(), Word.InsertLocation.start);

    // Synchronize the document state by executing the queued commands,
    // and return a promise to indicate task completion.
    return context.sync().then(function () {
        console.log('Added base64 encoded text to the beginning of the range.');
    });
})
.catch(function (error) {
    console.log('Error: ' + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log('Debug info: ' + JSON.stringify(error.debugInfo));
    }
});

Upvotes: 3

Related Questions