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%.
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%.