Log in

View Full Version : How to add columns to all tables in a Wd document



trombonegirl
02-21-2009, 07:12 PM
Hi everybody - forgive my newbieness. I'm very much a beginner, and know what I want to do but don't know how to say it.

I need to add a column to the left and right side of every table in a document, and fill those cells with <tr> and </tr> (yes, I'm trying to add HTML code to a Word document).

As a bonus, it would be great to add <td> to the beginning of the other (original) cells in the table, and </td> to the end.

The trouble is, the tables are all sorts of different sizes. (If this has been answered elsewhere please feel free to send me a link. I've been looking all day and I'm just getting a bit desperate. :help)

I have a related question -- when using the For Each in a document, do you need to go to the top of the document first with the Selection.HomeKey Unit:=wdStory?

Dim docTable As Table

Selection.HomeKey Unit:=wdStory

For Each docTable In ActiveDocument.Tables
With docTable

macropod
02-22-2009, 04:28 AM
Hi trombonegirl,

Here's some code that addresses your problem and answers your question:

Sub TableMod()
Dim oTbl As Table
Dim oCol As Column
Dim oCel As Cell
With ActiveDocument
For Each oTbl In .Tables
With oTbl
.Columns.Add BeforeColumn:=.Columns(1)
.Columns.Last.Select
Selection.InsertColumnsRight
For Each oCel In .Columns(1).Cells
oCel.Range.Text = "<tr>"
Next
For Each oCel In .Columns.Last.Cells
oCel.Range.Text = "</tr>"
Next
For Each oCol In .Columns
If oCol.Index > 1 And oCol.Index < .Columns.Count Then
For Each oCel In oCol.Cells
oCel.Range.Text = Replace(oCel.Range.Text, oCel.Range.Text, _
"<td>" & Left(oCel.Range.Text, Len(oCel.Range.Text) - 2) & "</td>")
Next
End If
Next
End With
Next
End With
End Sub

trombonegirl
02-22-2009, 04:54 PM
Yay!!!
:clap:
It's like Christmas in February. This is awesome - it does exactly what I
wanted it to do. What's even more cool is that seeing it all put together I
can read it and understand what it's doing - I had most of the pieces but
was lacking a few important bits (like the word "next" duh).

I even wrote a small piece of code, using what you gave me as a model, and
it worked the first time, wow. Seeing an example will help me tweak a couple
of other pieces of code I've been working on.

Thanks so much for taking the time to provide me with this. I promise to
help others here as soon as I know what I'm doing.

Karin

macropod
02-22-2009, 05:05 PM
Hi Karin,

You're welcome. Here's a more efficient version:

Sub TableMod()
Dim oTbl As Table
Dim oCel As Cell
With ActiveDocument
For Each oTbl In .Tables
With oTbl
For Each oCel In .Range.Cells
oCel.Range.Text = Replace(oCel.Range.Text, oCel.Range.Text, _
"<td>" & Left(oCel.Range.Text, Len(oCel.Range.Text) - 2) & "</td>")
Next
.Columns.Add BeforeColumn:=.Columns(1)
.Columns.Last.Select
Selection.InsertColumnsRight
For Each oCel In .Columns(1).Cells
oCel.Range.Text = "<tr>"
Next
For Each oCel In .Columns.Last.Cells
oCel.Range.Text = "</tr>"
Next
End With
Next
End With
End Sub

What makes this version more efficient is that, by processing the existing columns before adding the new ones, the IF test and secondary loop can be eliminated.


Have a nice day.

macropod
03-02-2009, 02:52 PM
Hi Karin,

The above code can be made yet more efficient by replacing:
.Columns.Last.Select
Selection.InsertColumnsRight
with:
.Columns.Add