-1

I'm using the default 'Excel web add-in' template in Visual Studio 2017. I'm trying to create an excel add-in that inserts a copy of an existing workbook into the current one. The first step is to get the full path and name of the current workbook. I got the code from here. I'm using the beta excel API. At the line 'var myFile = document.getElementById("file");' myFile is always null. I assume the null value is because the workbook isn't 'loaded' but the workbook does open when I run the program.

Here is the code from Home.js:

'use strict';

(function () {
    Office.onReady(function () {
        // Office is ready
        $(document).ready(function () {
            // The document is ready
            $('#RunMacroButton').click(RunMacro);
        });
    });

    function RunMacro() {


        var myFile = document.getElementById("file");
        var reader = new FileReader();

        reader.onload = (function (event) {
            Excel.run(function (context) {
                // strip off the metadata before the base64-encoded string
                var startIndex = event.target.result.indexOf("base64,");
                var workbookContents = event.target.result.substr(startIndex + 7);

                Excel.createWorkbook(workbookContents);
                return context.sync();
            }).catch(errorHandlerFunction);
        });

        // read in the file as a data URL so we can parse the base64-encoded string
        reader.readAsDataURL(myFile.files[0]);

    }
})();

1 Answers1

0

The line 'var myFile = document.getElementById("file");' is always null because there is no element called "file". This is also the wrong way to get the path of the currently open workbook. Instead use 'Office.context.document.url' to return the path.