user3307754
user3307754

Reputation: 21

Is there a correct way to save document from Javascript Api for Office in Word Desktop client?

Context

I am writing an Office Word Task Pane Addin using JavaScript API for Office (v 1.1).

My system :


Problem

The problem I am facing is about saving the opened document in the client machine. I have searched for information, but remain unsuccessful...

I would like to know if there is a way to save Word document from the JavaScript API, there are only a few methods in the Office.Context.Document :

enter image description here


Code

I wrote a solution but I dont like the way I used to save document by creating Word.Application Activex :

$scope.saveDocument = function (draft) {
    // Draft is a part of the window title
    // Example : 
    // ----------
    // If word window title shows "B0306-000-2012.docx - Word", 
    // draft could be "B0306-000-2012"
    try {
        var Word = new ActiveXObject("Word.Application");
        if (Word) {
            var spanish = Word.Language == 3082;
            var Tasks = Word.Tasks;
            for (var i = 0; i < Tasks.Count; i++) {
                var Task = Tasks.Item(i + 1);
                if (Task.Visible && Task.Name.indexOf(draft) >= 0) {
                    var shell = new ActiveXObject("WScript.Shell");
                    var activated = shell.AppActivate(Task.Name, 2000); //Espera 2 segundos para activar la aplicación
                    shell.SendKeys(spanish ? "^g" : "^s", 3000); //Envía el comando Ctrl + (G / S) según el idioma de la aplicación y espera 3 segundos a que Word guarde el documento
                    break;
                }
            }
            Word.Quit();
        }
        return true;
    } catch (e) {
        $OfficeApp.showModal("No se pudo guardar el documento", "Error: " + e.message);
        return false;
    }
};

Question

Any suggestion on how to save document using JavaScript API directly?

Upvotes: 2

Views: 4378

Answers (2)

Juan Balmori
Juan Balmori

Reputation: 5036

It seems that what you want to save is the current document, and as Cindy proposes this is not possible with 2013. It is possible in 2016 plus.

if you want to save a copy of the document (in 2103) you can do it. Note that what Cindy is suggesting is not possible, you can get the entire file in 3 formats: compressed (docx), plain text or PDF, you cannot get the XML unless the user is selecting the entire document. What you need to do is to use the getFileAsync() method (here is a sample) make sure to ask for the compressed format that will give you slices of the docx file, then you can save or upload this file wherever you need to.

Upvotes: 0

Cindy Meister
Cindy Meister

Reputation: 25663

The Office JS APIs (currently) for Office 2013 have no interface for opening or saving documents.

The only workaround that occurs to me would be to extract the OOXML and "stream" that to a new file (with the file extension xml). But there are certain things the OOXML does not contain (such as document properties) so depending on what content your documents have that might not be an option.

Edit from discussion in Comments:

For anything else, it's necessary to use the old-style VSTO/COM/VBA add-in which offers full object model functionality.

Upvotes: 1

Related Questions