code it

Add table number to each table from # table onwards in Word

I had a word document with many tables. The task was to add an ID (#) in the first row of the table. The insersion of the ID needed to start from certain Table onwards. Came up with a macro which when executed – will popup an input box – type the table number from where the ID needs to be inserted from and click ok.

Option Explicit
Sub AddTableID()
Dim tableIndex As String
Dim oDoc As Document
Dim oTables As Tables
Dim oTable As Table
Dim oRow As Row
Dim totalTables As Long
Dim t As Long
Dim tableID As Long
tableID = 1
tableIndex = InputBox(Prompt:="Add Table ID from which Table onwards", Title:="", Default:="")
If tableIndex = vbNullString Then
' dont do anything
Set oDoc = ActiveDocument
Set oTables = oDoc.Tables
totalTables = oTables.Count
For t = tableIndex To totalTables
Set oTable = oTables(t)
'oTable.Rows.Add BeforeRow:=oTable.Rows(1)
Set oRow = oTable.Rows(1)
oRow.Cells(1).Range.Text = "Table ID : " & tableID
tableID = tableID + 1
Next t
Set oRow = Nothing
Set oTable = Nothing
Set oTables = Nothing
Set oDoc = Nothing
End If
End Sub

add table id


December 26, 2010 - Posted by | VBA, Word

No comments yet.

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: