1

I am developing a word add-in using word JavaScript api, I need to insert some data in table format inside the content Control and placed that content control on top of document. Please advice.

Thanks.

1 Answer 1

1

This should be quite a simple operation. I am assuming that by "On top" of the document you mean inserting a table where the document starts. First line.

All the insertion methods have an insertionLocation parameter that you can use for that purpose. On this case you want to do a body.isnertTable, the 3rd parameter is the insertionLocation ("start" is sent to insert at the beginning of the body of the document).

Once its inserted you can actually wrap it with a content control. Check sample below for details. I included other details, such as applying a built-in style to the inserted table.

hope this unblocks you. thx!

function insertTableAndWrapWithCC() { 
     Word.run(function (context) {
          // We need a 2D array to hold the initial table values
          var data = [["Apple", "Orange", "Pineapple"],["Tokyo","Beijing", "Seattle"]];
          var table = context.document.body.insertTable(3, 3, "start", data);
          table.styleBuiltIn = Word.Style.gridTable5Dark_Accent2;
//now we insert the content control on the table.
          var myContentControl = table.insertContentControl();
          myContentControl.title = "CC Title";
          return context.sync()
     })
     .catch(function (e) {
     	       console.log(e.message);
              })
     }

Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou for your response, it's working as expected. How the table content will edit, as i am loading the CC using Tag and want to replace the table content with new values. Please advice. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.