How do I end a table?

R

Rhino

I'm trying to create a series of tables in a VBA macro in Word 2002. Each of
the 6 tables is laid out the same way and each table has only two rows; the
first of those rows has 4 columns and the second row has only one column.

I have no problems creating and populating the first table but when I get to
the second table, rather than being created _after_ the first table, it is
being created _WITHIN_ the first table: a third row is created in the first
table and the second table is created on that row. Then the third table gets
created within the second and so on.

Once I've finished populating the cells of the first table, how do I make
the macro create the second table so that it is AFTER the first table, not
within it?

I've tried recording a macro to learn the correct code but I can't seem to
end a table while the macro recorder is running.

The other day, I was having a comparable problem ending a list - once I'd
started a list, all following content appeared in the list - and was told to
use Selection.Range.ListFormat.RemoveNumbers. I've looked for something
comparable to end a table and hoped to find something like
Selection.Range.TableFormat.EndTable but no such thing exists.

What's the trick I need to know? It certainly isn't intuitive, whatever it
is!
 
D

Dave Lett

Hi,

Try something like the following (assuming that your cursor is in the table
that you want to end):

With Selection
.Tables(1).Select
.MoveRight
.TypeParagraph
End With

It's more efficient, probably, to do all of this with ranges, but that just
opens another can of worms.

HTH,
Dave
 
R

Rhino

That didn't work for me but that may very well be my fault for not
explaining my situation in more detail. Let me elaborate a bit and maybe
you - or someone else - can tell me what I'm doing wrong.

The document I am writing is my resume. The tables I'm writing each describe
one of the six jobs I've had. Each table contains 5 columns: the start/end
dates for the job; the employer name; my role in that job; the main tools I
used; and a description of what I did at that job. The first four of the
items in each table are short so they go into a single row of the table. The
description is a full sized paragraph so it goes in the last cell of the
table. Each table is a new, separate table by itself, i.e. none of the
tables are nested within one another. Therefore, the document will look like
this:

EMPLOYMENT HISTORY
------------------------------------------------------------------------------------------------
| 1997-now | ABC Inc. | Programmer | HTML, C
|
------------------------------------------------------------------------------------------------
| Invented new processes. Wrote amazing programs. Fetched coffee. |
------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------
| 1994-1996 | XYZ Corp. | Programmer | HTML, C
|
-------------------------------------------------------------------------------------------------
| Invented new processes. Wrote amazing programs. Fetched donuts. |
-------------------------------------------------------------------------------------------------

etc.

Okay, now that you understand what I'm trying to do, here is the relevant
code.

-----------------------------------------------------------------------------------------------------------
'For each job in the job array, create a small table and write the facts for
that job into the table.
For jobNumber = 0 To sizeJobArray - 1
If (TRACE) Then MsgBox jobNumber, vbOKOnly, "Job Number"
'Create a table for this job's facts.
createTable (jobNumber)
jobString = jobArray(jobNumber)
factArray = Split(jobString, SPLIT_CHAR_TILDE, -1, vbTextCompare)
sizeFactArray = UBound(factArray)
For factNumber = 0 To sizeFactArray
If (TRACE) Then MsgBox factNumber, vbOKOnly, "Fact Number"
Selection.TypeText Text:=factArray(factNumber)
Selection.MoveRight unit:=wdCell
Next factNumber

Selection.TypeParagraph

Next jobNumber
-----------------------------------------------------------------------------------------------------------

Your suggestion was essentially to do a TypeParagraph after writing the last
cell of the table so that's the second last line above. But, despite doing
that, the second table is still created within the first table rather than
after it.

This is the createTable sub:

-----------------------------------------------------------------------------------------------------------
Sub createTable(jobNumber As Integer)

If (TRACE) Then MsgBox jobNumber, vbOKOnly, "Job Number"

'Create a two row, 4 column table. Merge the cells in the second row into a
single cell.
Set newTable = ActiveDocument.Tables.Add(Range:=Selection.Range,
NumRows:=2, NumColumns:= _
4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed)

With newTable
If .Style <> "Table Grid" Then
.Style = "Table Grid"
End If
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
.Rows(2).Cells.Merge 'Merge the cells on the second row into a single
cell
End With

End Sub
-----------------------------------------------------------------------------------------------------------

So, what do I need to do differently to make each table be a new,
independent table rather than a table that is nested within the previous
table?

I thought the answer might lie in one of the attributes of
ActiveDocument.Tables.Add. For instance, maybe I need a different value for
DefaultTableBehavior. I'm darned if I can find anything in the help that
describes what behavior DefaultTableBehavior causes so I don't know if it is
culprit. Or maybe I need something that effectively says, "Okay, we're
finished with that table so close it; then the next table will _NOT_ be
nested.
 
D

Dave Lett

Hi,

<snip>Your suggestion was essentially to do a TypeParagraph after writing
the last
cell of the table so that's the second last line above. But, despite doing
that, the second table is still created within the first table rather than
after it.</snip>
Not exactly. My suggestion was to select the current table, move the
selection to the right, then to type a paragraph. All that you did was type a
paragraph, which would not move reliably OUT of the table. So, replace

Selection.TypeParagraph
with
With Selection
.Tables(1).Select
.MoveRight
.TypeParagraph
End With

HTH,
Dave
 
R

Rhino

Thanks, Dave, that worked perfectly!

Obviously, I should have listened to you the first time rather than adapting
your advice ;-)

I really expected to run into trouble with 'Selection.Tables(1).Select',
which is why I adapted your advice rather than following it literally. I
thought that 'Selection.Tables(1).Select' would always refer to the _first_
of the 6 tables and therefore only end the first table cleanly but it
obviously works for all 6 tables despite the hard-coded subscript. I
probably shouldn't be looking a gift horse in the mouth but why doesn't the
hard-coded subscript give me trouble?

I've been programming for many years but I'm new to VBA so I'm still trying
to understand some of what I'm seeing in this language.
 
D

Dave Lett

Hi,

What do you mean by hard-coded subscript? Do you mean
With Selection
.Tables(1).Select
.MoveRight
.TypeParagraph
End With

If so, it doesn't give you trouble because it's the selection. That is,
where is the cursor when you use the selection object? It's in the table.
Therefore,
Selection.Tables(1).Select
means select the first table that is in the current selection. It doesn't
give you problems because there is only one table in the current selection.
Play around with it until you get comfortable with the idea. For example, in
a document create several tables. Manually highlight the middle 3 or 4
tables, and then explore the differences of

Selection.Tables(1).Select
Selection.Tables(2).Select
Selection.Tables(3).Select

Create a table within a table. Place the cursor in the "interior" table and
run
Selection.Tables(1).Select
Place the cursor in the "exterior" table and run
Selection.Tables(1).Select
Manually highlight the "exterior" so that the "interior" table is also
highlighted and run
Selection.Tables(1).Select

Notice, finally, that each of these differs from using the ActiveDocument
object.
ActiveDocument.Tables(x).Select, where x is a valid number for the number of
tables you might have in a document.

HTH,
Dave
 
R

Rhino

Okay, I get it now.

The "Selection." in front of "Tables(1)" limits the scope of the .MoveRight
and .TypeParagraph so that they are only in the current table.

I was thinking of Tables(1) working like ActiveDocument.Tables(1) but
"Selection." narrows the scope.

Okay, that makes sense.

Thanks for the explanation!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top