1

I'm developing a Word Add-in (Word API + Office.js) where i am working with content controls, I am trying to read the table content inside a content control where I need remove the empty rows Sample: I have this table inside a content control I have to remove the blank rows

enter image description here

I tried the below code to read the table content but getting undefined error at reading table

   function checktable() {
    Word.run(function (context) {
        // Queue a command to get the current selection and then
        // create a proxy range object with the results.
        var contentControls = context.document.contentControls.getByTag('control').getFirst();       
        context.load(contentControls,'tables');

        return context.sync()
            .then(function () {
                var table;
                // Get the longest word from the selection.
                if (contentControls.tables.items.length === 0) {
                    document.getElementById('lblstatus').innerText += "No Tables found";
                }
                else {
                    document.getElementById('lblstatus').innerText += " Tables found";
                    table = contentControls.tables.getFirstOrNullObject();

                }

                context.load(table, 'values');


            })
            .then(context.sync)
            .then(function () {

                var Tablevaules = table.values;


                // Queue a command to highlight the search results.
                document.getElementById('lblstatus').innerText += element + ":" + "Successs";

            });
    })
        .catch(errorHandler);
} 

Please let me know whether any other way to achieve this functionality or is it possible using office js

Common_Coder
  • 131
  • 6

1 Answers1

0

I think I see three problems in your code:

  1. The var you are calling contentControls is actually a single content control. (The first one with the tag "control".) The plural name is confusing and makes your code hard to parse.

  2. The following line in your code has a problem:

    context.load(contentControls,'tables');
    

    The tables property is a collection property. You can't load collection properties. You need to load the property (or properties) of the members of the collection; that is, the tables in the tables.items array. It looks like you need to load the length property, since that is the one that your code reads after the sync. I recommend the book Building Office Add-ins, especially the section Loading Collections for more information.

  3. When you use an *OrNullObject method, you need to call a context.sync and then test the object to see if it's null. See orNullObject Methods.

Rick Kirkham
  • 9,038
  • 1
  • 14
  • 32