Saturday, March 6, 2010

VBScript - Creating table in a word file

VBScript - Creating table in a word file

Creating a table in a word document

Const rows = 1
Const columns = 3

Set word = CreateObject("Word.Application")
word.Visible = True
Set objectdocument = word.Documents.Add()

Set objRange = objectdocument.Range() 'Instance of Range object needed to create a table

'Table.Add takes 3 arguments, (objRange) - signifies the document location where we want to put the table, then number of rows and then number of columns
objectdocument.Tables.Add objRange, rows, columns

Set objecttable = objectdocument.Tables(1) 'creating object reference to the table, 1 is the first table in the tables collection

objecttable.AutoFormat(16) 'argument 16 means that we want to show the table grids.

Creating a table in a word document and writing text to it

Const rows = 1
Const columns = 3

Set word = CreateObject("Word.Application")
word.Visible = True
Set objectdocument = word.Documents.Add()

Set objRange = objectdocument.Range() 'Instance of Range object needed to create a table

'Table.Add takes 3 arguments, (objRange) - signifies the document location where we want to put the table, then number of rows and then number of columns
objectdocument.Tables.Add objRange, rows, columns

Set objecttable = objectdocument.Tables(1) 'creating object reference to the table, 1 is the first table in the tables collection

objecttable.Cell(1, 1).Range.Text = "ABC" 'this text will go in Row 1 & Column 1
objecttable.Cell(1, 2).Range.Text = "DEF" 'this text will go in Row 1 & Column 2
objecttable.Cell(1, 3).Range.Text = "GHI" 'this text will go in Row 1 & Column 3

objecttable.AutoFormat(16) 'argument 16 means that we want to show the table grids.


Adding rows to the table - we are taking the script (Creating a table in a word document) and adding three more rows to the table already created by that.

Const rows = 1
Const columns = 3

Set word = CreateObject("Word.Application")
word.Visible = True
Set objectdocument = word.Documents.Add()

Set objRange = objectdocument.Range() 'Instance of Range object needed to create a table

'Table.Add takes 3 arguments, (objRange) - signifies the document location where we want to put the table, then number of rows and then number of columns
objectdocument.Tables.Add objRange, rows, columns

Set objecttable = objectdocument.Tables(1) 'creating object reference to the table, 1 is the first table in the tables collection

objecttable.Rows.Add()
objecttable.Rows.Add()
objecttable.Rows.Add()

objecttable.AutoFormat(16) 'argument 16 means that we want to show the table grids.

Also See:

Opening a new word document
Opening an existing word document
Opening a new word document and writing text to it
Opening an existing word document and appending text to it at the end.
Opening an existing word document and appending text to it at the beginning.
Saving a new word file
Saving an existing word file
Writing formatted text to a word file
Formatting all content, formatting specific paragraphs or finding and formatting specific text in a word document
Inserting images/pictures to a word file
Printing a word file