1

I am using the code below to apply a custom paragraph style called "Table: Body Text" to text in tables, but it doesn't apply the style to all the selected text. For example, if I only select a column, and click my style button, it only applies the style to the last row in the selected column not all the rows in the selected column. Seems a bit random.

A couple of weeks ago, it was only applying the style to one of the many rows I had selected, but that seems to have resolved - perhaps with exiting out and going back in. So the column is the main issue now.

For example if I want to select three cells in the first column to apply a Row Heading style, it will only apply the style to the last cell in the set of selected rows in the first column. Same with wanting to apply a Table Bullet style to the last column - it won't do it except one by one.

Apologies, I just don't know enough about the Javascript API and how Javascript works to know where to begin to get this code to do what I need it to do specifically.

Thanks!

function applytablebodytextstyle() {
    Word.run(function (context) {

  var pars = context.document.getSelection().paragraphs;
  pars.load();
        return context.sync().then(function () {
            for (var i = 0; i < pars.items.length; i++) {
                pars.items[i].style = "Table: Body Text";
    
            }
  
        return context.sync();
    })
 }) //needed for Stack overflow
    .catch(function (error) {
        console.log("Error: " + error);
        if (error instanceof OfficeExtension.Error) {
            console.log("Debug info: " + JSON.stringify(error.debugInfo));
        }
    });
}
  • I've requested that this be reopened. It is not a duplicate of the other question. You can't get an accurate collection of Paragraph objects across table cells. Please work with the Word.Table* classes, especially [TableCell](https://learn.microsoft.com/en-us/javascript/api/word/word.tablecell?view=word-js-preview).body.style. – Rick Kirkham Aug 06 '19 at 16:50
  • Thanks Rick - I've searched some more here in stackoverflow and still haven't found a solution to this problem. I suspect it might be a bit outside my skill level yet. I don't understand how JavaScript thinks. – user3029745 Aug 07 '19 at 10:13
  • I tried changing my getSelection statement to: var pars = context.document.getSelection().parenttablecell.body.range; and it behaved in the same way as my original code. It seems odd to me that treats a selected range of cells in all or part of a column, or part of a row differently to the entire table, or set of entire rows. It seems I need to work out how to loop through all cells - but that would likely have a time cost on large tables. – user3029745 Aug 07 '19 at 10:26
  • I did notice something odd-ish - if I select all the cells in the row, but not the end of row marker, the code treats this the same as the column and only formats the last row cells in the selection. Whereas, when I include that end of row marker, it formats all cells in the selected row. A column wouldn't have that. It's also fine with the original code in apply the style to text or selected paragraphs within a single cell; – user3029745 Aug 07 '19 at 10:26
  • The body of the `for` loop does not do anything asynchronous, so it is not a duplicate of the Closure Inside Loops question. – CertainPerformance Aug 10 '19 at 00:11

0 Answers0