Need help sizing table columns correctly

R

Rhino

I need some help getting a macro working exactly the way I want. I've looked
in the Help but can't quite figure out what I need to do.

I'm creating a series of small tables, that will appear one after the other.
Each table will have two rows. The first of the two rows will have 4
columns, the second row will contain only one column.

I'd like each table to use all of the available width between the left and
right page margins, which are set at 60 points each. I want the first three
columns of the first row of each table to have a specific fixed width. I'd
like the fourth column of the first row of each table to get whatever space
is left over after the first three columns have been written. The second row
of each table should get the full available width between the left and right
page margins.

This is my current sub for creating the table and setting the desired column
widths:

=================================================================
Sub createTable()

'Create a two row, four column table.
Set newTable = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=2,
NumColumns:= _
4, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
wdAutoFitFixed)

'Format the table. Set the column widths and merge the cells on the second
row into a single cell.
With newTable
If .style <> "Table Grid" Then
.style = "Table Grid"
End If
.AllowAutoFit = False
.PreferredWidthType = wdPreferredWidthPercent
.PreferredWidth = 100
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False
'Set widths of columns (in inches)
.Columns(1).Width = InchesToPoints(1.1)
.Columns(2).Width = InchesToPoints(1.2)
.Columns(3).Width = InchesToPoints(1.7)
.Columns(4).Width = InchesToPoints(3.2)
'Merge the cells on the second row into one cell
.Rows(2).Cells.Merge
End With

'Make sure table isn't split across pages.
Selection.Tables(1).Select
With Selection.ParagraphFormat
.KeepWithNext = True
End With


End Sub
=================================================================

This _almost_ does what I want but there are two things that I don't like:
a. "AutoFitBehavior:=wdAutoFitFixed" in the 'set' statement and
".AllowAutoFit = False" seem to contradict one another; the 'set' seems to
be saying that I should use AutoFit while the 'with' seems to turn it off.
Should the 'set' be written differently to prevent the columns from ever
having had the ability to resize themselves?
b. I want the widths of the first 3 columns on the first row of each table
to be fixed at the sizes shown. I want the 4th column to automatically get
whatever space is left between the 3rd column and the right margin without
first having to calculate an actual length in inches. I thought that by
leaving the width of the 4th column unspecified, it would get all of the
remaining space but, in fact, it gets quite a bit less than the available
space, with the result that the table is narrower than it needs to be and
the right margin is much bigger than I want in the area where the tables are
written. How can I get that behavior?

I've looked in the Help but I must be searching on the wrong keywords
because I can't find a clear answer to either problem.

I was going to do some 'trial and error' with the width of the 4th column. I
erased the line after the "=" on the line that sets column 4's width but the
other options never appear. I was hoping to see something like
"UseRemainingSpace". Shouldn't I be getting a list of options when I do
that? If yes, I must have something wrong in my VBE settings; can someone
tell me what I need to turn on?

If you can help me solve these last two problems, I think my document will
be absolutely perfect instead of just 98%.
 
D

Doug Robbins - Word MVP

Create a table of the sort that you want and then make an autotext entry out
of it.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

Rhino

How? Do you mean that I should record this with the macro recorder and then
figure out what needs to change in my existing macro to match the new
recording? If so, I'm a little skeptical of that approach: I've been burned
several times now by the macro recorder not capturing things that I've done
while it was running.

Or have I misunderstood your suggestion?

--
Rhino

Doug Robbins - Word MVP said:
Create a table of the sort that you want and then make an autotext entry
out of it.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
D

Doug Robbins - Word MVP

Yes, I think that you have mis-understood my suggestion. There is no need
for a macro. Here is what you do

1 Set up a single table the way the way you want it
2 Select that table and then from the Tools menu, select AutoCorrect
Options and then go to the AutoCorrect tab and it the "Replace text as you
type" section in the With area, you will see a couple of table cells. In
the Replace control to the left, type something like mytable, make sure that
the "Formatted text" radio button above the table cells is select and then
click on the Add button.

After doing this, whenever you type mytable and press the space bar, a
replica of your table will be inserted in its place.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Rhino said:
How? Do you mean that I should record this with the macro recorder and
then figure out what needs to change in my existing macro to match the new
recording? If so, I'm a little skeptical of that approach: I've been
burned several times now by the macro recorder not capturing things that
I've done while it was running.

Or have I misunderstood your suggestion?
 
R

Rhino

Okay, I see what you're saying and agree it would be a useful technique for
some situations. However, I want to do this work via a macro in this case,
which is why I posted to a VBA newsgroup in the first place.

I am building this document from scratch each time and I don't even want to
make Word GUI visible while the document is generated. There is a way to do
what I want by writing statements in a macro, right? Or is your approach the
only way that I can size my columns correctly?
 
D

Doug Robbins - Word MVP

Create an autotext entry of the table and then use vba to insert the
autotext.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
R

Rhino

Okay, how do I use VBA to insert autotext? I think you've already told me
how to create the autotext entry.

Is there no way to do both things programatically, i.e. with VBA statements?
That's what I really want to do.... You see, the document could get deleted
at some point and have to be re-created entirely from the macro. If the
autotext disappears when the document is deleted, I'd have to figure out how
to re-create it again. If I just put *ALL* of the logic in the VBA code, I'm
covered.
 
D

Doug Robbins - Word MVP

What I told you before was how to create an autocorrect for the table. The
procedure to create an autotext is similar. but you go to the Autotext tab
instead of the Autocorrect tab in the Tools Autocorrect Options dialog.
Look up InsertAutotext in the vba help file for how to use it.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 

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