Thursday, November 17, 2011

Find row and column number of table(cells) existing in word document and add value to the cells from Excel

If you want to copy the value from excel to table existing in a word document the first thing which we have to do is to find out is the row numbers and column numbers of cells of word Table.

The below code will help us to find out the row no and column no of cells in a tables

Sub find_row_no_and_col_no_of_word_table()
Dim doc As New Word.Application
Dim cll As Word.Cell
doc.Visible = True
' TOOL -> REFRENCE-> MICROSOFT WORD
doc.Documents.Open "C:\Documents and Settings\achamanlalko\Desktop\blank Table.doc"
' below code u can use to find out the cell row and col in the table
For Each cll In doc.ActiveDocument.Tables(1).Range.Cells

cll.Range.Text = cll.Range.Text & " r--" & cll.RowIndex & " c--" & cll.ColumnIndex
Next cll
End Sub

Download blank Table.doc document



Once we know the row no and col we can use the below code to add data to that particular table

Sub add_data_word_table()
Dim doc As New Word.Application
Dim cll As Word.Cell
doc.Visible = True
' TOOL -> REFRENCE-> MICROSOFT WORD
doc.Documents.Open "C:\Documents and Settings\achamanlalko\Desktop\blank Table.doc"
' below code u can use to find out the cell row and col in the table

doc.ActiveDocument.Tables(1).Cell(2, 1).Range.Text = "www.excelvbamacros.com"
doc.ActiveDocument.Tables(1).Cell(3, 1).Range.Text = Sheets(1).Range("a1").Value
End Sub

Download Excel Macro

1 comment:

Import data from SQL

Macro to import data from SQL using ADO connection string: Sub Import_data_from_SQL() ' Tools -> References -> Microsoft Active...