Jerry Weeks
Jerry Weeks

Reputation: 315

Is it possible to insert a base64 Word file using Office 2013 office api

I have a successfully running script that loads Word files from SharePoint and inserts them into Word 2017 (Office 365 Word local client, not online) The current scripts reads up the files using Ajax and extracts the base64 file and uses

body.insertFileFromBase64(myBase64, end)

I now need to extend the functionality to support Word 2013 (i.e. use the Office.js instead of the Word JavaScript api). So the code has changed to

Office.context.document.setSelectedDataAsync(file, someCoercionType)

I hoped to be able to use a variant of

Office.context.document.setSelectedDataAsync(myBase64, {coercionType: Office.CoercionType.Ooxml}, function (

But I get an error back "The Format of the specified data object is invalid", which is correct enough as the Office API assumes a base64 file is an image.

Is it possible to convert the Base64 file to XML in JavaScript? (Elsewhere in my code I unzip the docx and extract bookmarks, but only from document.xml which lacks all formatting and images, footers etc.)

Upvotes: 1

Views: 2018

Answers (1)

Marc LaFleur
Marc LaFleur

Reputation: 33114

Base64 is simply an binary encoding and blissfully unaware of the underlying content type. So if you're source content was OOXML, decoding it would give you that OOXML back. What you cannot do is type conversion. For example, a Base64 encoded JPEG can not be decoded directly into a BMP. To do that you would need to first decode and then convert from JPEG to BMP using some other tool.

If you're seeking to manipulate or extract content an existing document, you may want to look at Aspose.Words. Aspose provides tools that allow you to programmatically work with Word documents (they have similar tools for a flew of other file types as well). Using this, you may be able to extract the OOXML you're looking for so you can then insert it into Word using Office.js.

At the moment, the only Coercion Type that accepts Base64 encoded content is Office.CoercionType.Image.

Upvotes: 0

Related Questions