Integration with word – Part 2

In this section let us learn to do following things in integration with word
    • Creating tables
    • Applying Styles to tables
    • Adding rows to tables
    • Getting cells and filling data

Creating Tables

To add a table first we need to get the current range (it can be either start of the document or last cell of the document). Let us assume that we have start of the document. So your code should look something like this

Range = Document.Range(0, 0);

Where ‘Range’ and ‘Document’ are variables of type COM. Now After getting the range of the document to insert the table, we need to get the reference to tables from the document so you can write the code as follows

Tables = Document.Tables();

Now add a table at the specified range. To do this you need to make a call to method add() of ‘Tables’ object. The method add takes three arguments 1. Range – position to insert 2. Rows – Number of rows, 3. Columns – Number of columns. Below is the line of code to be written

Table = Tables.Add(range , 1, 3);

Applying Styles to Tables

To apply a particular style to table, you first need to know the name of the style. This you can find out from Styles manager in word. Then you just need to write following code to apply the style‘Table Grid’);

Adding Rows to Table

To add a row to table we first need to get all the rows from the table and then call add method. Just write following code

rows = table.rows();

Getting cells and filling data

Write following code to get a cell and fill data.

cell = table.cell(1, 1);
range = cell.range();


Following example code illustrate following

    • Open a word document
    • Add a table
    • Write to cells
    • Add rows to table
    • Apply styles to table
    • Format the first row of the table

static void writeTableInWord(Args _args)
    COM                 app, document, wordDocument;
    COM                 range, tables, table;
    COM                 cell, cells, rows, row, shading, font, paragraphs;
    int                 i, j;
    SysDictTable        sysDictTable = new SysDictTable(tableNum(CustGroup));
    SysDictField        sysDictField;

    app = new COM(‘Word.Application’);
    document     = app.Documents();
    wordDocument = document.add();
    range = wordDocument.range(0,0);
    tables = wordDocument.tables();
    //Add the table
    table = tables.add(range , 1, 3);‘Table Grid’);
    //Write to cells
    cell = table.cell(1,1);
    range = cell.range();
    range.text(‘Field Name’);

    cell = table.cell(1,2);
    range = cell.range();

    cell = table.cell(1,3);
    range = cell.range();

    for (i = 1; i <= sysDictTable.fieldCnt(); i++)
        sysDictField = sysDictTable.fieldObject(i);
        if (sysDictField && !sysDictField.isSystem())
            //Add a row to the table
            rows = table.rows();
            cell = table.cell(i + 1, 1);
            range = cell.range();
            cell = table.cell(i + 1, 2);
            range = cell.range();
            cell = table.cell(i + 1, 3);
            range = cell.range();

    //Formatting first row of the table
//Get the first row
    row = rows.first();
    cells = row.cells();
    cells.verticalAlignment(1); //Middle
    row.setHeight(20, 2);
    shading = row.shading();
    shading.backgroundPatternColor(10526880); //Grey 375
    range = row.range();
    font  = range.font();
    font.bold(1); //Bold
    paragraphs = range.paragraphs();
    paragraphs.alignment(1); //Center

After running the job following output can be seen in the word document

Field Name





Group of customers.



Describe the customer group



Terms of payment for period from due date to payment.



Terms of payment used with forecast coverage plan for the group


See Also: Integration with word

One thought on “Integration with word – Part 2

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s