none
How to fix Office.context.document undefined error RRS feed

  • Question

  • I am opening an existing Excel workbook and then adding a task pane app to it. While using Visual Studio 2013 debugger, the app comes up but it doesn't seem to know about the Excel workbook that is open. So, I get a Office.context.document undefined error. What do I need to add to the Office.initialize() function to get the app talking to the open Excel document? All the code comes from online sources.

    /// <reference path="../App.js" />
    
    (function () {
        "use strict";
    
        // The initialize function must be run each time a new page is loaded
        Office.initialize = function (reason) {
            $(document).ready(function () {
                if (reason == "inserted") {
                    writeToPage("the app was just inserted");
                };
    
                app.initialize();
    
                $('#get-data-from-selection').click(getDataFromSelection);
    
                // rest of code came from: http://msdn.microsoft.com/library/office/fp142161.aspx#FirstAppWordExcelVS_Create
    
                // writes something hardcoded in writeData function to screen - use for testing only
                $("#writeDataBtn").click(function (event) {
                    writeData();
                });
    
                // user must have cell(s) selected - this function shows selected data in task pane app results
                $("#readDataBtn").click(function (event) {
                    readData();
                });
    
                // user must have cell(s) selected - this function binds a range to a name so it doesn't have to be selected in the future
                $("#bindDataBtn").click(function (event) {
                    bindData();
                });
    
                // user does not have to have cells selecteed - this function shows previously bound data in task pane app results
                $("#readBoundDataBtn").click(function (event) {
                    readBoundData();
                });
    
                // this function adds handler to bound data so it reports data to task pane app results every time is changes in Office
                $("#addEventBtn").click(function (event) {
                    addEvent();
                });
            });
            writeToPage("office initialize complete");
        };
    
        function writeData() {
            Office.context.document.setSelectedDataAsync([["red", "rojo"], ["green", "verde"], ["blue", "azul"]], function (asyncResult) {
                if (asyncResult.status === "failed") {
                    writeToPage('Error: ' + asyncResult.error.message);
                }
            });
        };
    
        function writeToPage(text) {
            document.getElementById('results').innerText = text;
        };
    
        function readData() {
            Office.context.document.getSelectedDataAsync("table", function (asyncResult) {
                if (asyncResult.status === "failed") {
                    writeToPage('Error: ' + asyncResult.error.message);
                }
                else {
                    writeToPage('Selected data: ' + asyncResult.value);
                }
            });
        };
    
        function bindData() {
            Office.context.document.bindings.addFromSelectionAsync("matrix", { id: 'myBinding' },
                function (asyncResult) {
                    if (asyncResult.status === "failed") {
                        writeToPage('Error: ' + asyncResult.error.message);
                    } else {
                        writeToPage('Added binding with type: ' + asyncResult.value.type + ' and id: ' +
                            asyncResult.value.id);
                    }
                });
        };
    
        function readBoundData() {
            Office.select("bindings#myBinding").getDataAsync({ coercionType: "matrix" },
                function (asyncResult) {
                    if (asyncResult.status === "failed") {
                        writeToPage('Error: ' + asyncResult.error.message);
                    } else {
                        writeToPage('Selected data: ' + asyncResult.value);
                    }
                });
        };
    
        function addEvent() {
            Office.select("bindings#myBinding").addHandlerAsync("bindingDataChanged", myHandler, function (asyncResult) {
                if (asyncResult.status === "failed") {
                    writeToPage('Error: ' + asyncResult.error.message);
                } else {
                    writeToPage('Added event handler');
                }
            });
        }
    
        function myHandler(eventArgs) {
            eventArgs.binding.getDataAsync({ coerciontype: "matrix" }, function (asyncResult) {
    
                if (asyncResult.status === "failed") {
                    writeToPage('Error: ' + asyncResult.error.message);
                } else {
                    writeToPage('Bound data: ' + asyncResult.value);
                }
            });
        }
        // Reads data from current document selection and displays a notification
        function getDataFromSelection() {
            Office.context.document.getSelectedDataAsync(Office.CoercionType.Text,
                function (result) {
                    if (result.status === Office.AsyncResultStatus.Succeeded) {
                        app.showNotification('The selected text is:', '"' + result.value + '"');
                    } else {
                        app.showNotification('Error:', result.error.message);
                    }
                }
            );
        }
    })();

    Tuesday, December 30, 2014 8:10 PM

Answers

  • Hi S.Jenks,

    Thanks for the detail explaintation.

    >>I don't see a Start Document property. I'm using VS 2013. I do have a Start Action property. Start Action is a drop down. In that I have tried picking Microsoft Excel - in which case the existing file is not opened, and picking the name of the existing document, in which case I get the problem I am having.<<

    Here is a screenshot for use Start Document for your reference:

    Then you can press F5 to start debug the project and when the document was opened, you need insert the app manually.

    To show an app for Office in Excel or Word

    1. In Excel or Word, on the Insert tab, choose Apps for Office.

    2. In the list that appears, choose your app.

    To show an app for Office in Project

    1. In Project, on the Project tab, choose Apps for Office.

    2. In the list that appears, choose your app.

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, January 1, 2015 2:52 AM
    Moderator

All replies

  • Hi Jenks,

    Thanks for posting in MSDN forum.

    I am trying to reproduce the issue however failed. How did you debug the app for an existing workbook?

    Here is an article that about debugging apps in Visual Studio for your reference and you can get more detail about debuging using existing workbook in To use an existing document to debug the app section:

    Debug your app in Visual Studio

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, December 31, 2014 8:39 AM
    Moderator
  • I followed the instructions on this link for Debug you app but ran into a difficulty. Under the section:

    To use an existing document to debug the app

    Step 6: In the Start Document list, choose the document that you added to the project, and then choose the OK button to close the property pages.

    I don't see a Start Document property. I'm using VS 2013. I do have a Start Action property. Start Action is a drop down. In that I have tried picking Microsoft Excel - in which case the existing file is not opened, and picking the name of the existing document, in which case I get the problem I am having.

    Thank you in advance for your help.

    S. Jenks

    Wednesday, December 31, 2014 2:02 PM
  • Hi S.Jenks,

    Thanks for the detail explaintation.

    >>I don't see a Start Document property. I'm using VS 2013. I do have a Start Action property. Start Action is a drop down. In that I have tried picking Microsoft Excel - in which case the existing file is not opened, and picking the name of the existing document, in which case I get the problem I am having.<<

    Here is a screenshot for use Start Document for your reference:

    Then you can press F5 to start debug the project and when the document was opened, you need insert the app manually.

    To show an app for Office in Excel or Word

    1. In Excel or Word, on the Insert tab, choose Apps for Office.

    2. In the list that appears, choose your app.

    To show an app for Office in Project

    1. In Project, on the Project tab, choose Apps for Office.

    2. In the list that appears, choose your app.

    Hope it is helpful.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, January 1, 2015 2:52 AM
    Moderator
  • Hi S.Jenks,

    Have you  fix the issue now? If you still have the issue, please feel free to let us know.

    Regards & Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, January 9, 2015 9:06 AM
    Moderator