how to adjust tables in one page

S

sam

Hi,

I have two tables in one specific page of Word Document. And these two
tables occupy the whole page. One table is named "item info", the other
one is named "comments". The size of the table "comments" depends on
the size of the table "item info". For instance, If I delete one row
in the table "item info", the size of table "comments" should be
enlarged one row in order to cover the whole page. I know how to delete
row in table using VBA.I am wondering whether there are some method to
enlarge the table automatively using VBA. Could you give me some hints
about this issue. Thank you in advances!!!

Best Regards

Sam
 
M

macropod

Hi Sam,

Let's say the "item info" table has a default number of rows (5) , as does
the "comments" table (4). That adds up to 9 rows, which is how many rows you
need to keep.

So, if you add 2 rows to the "item info" table, it's now got 7 rows and the
"comments" table needs to have 9-7=2 rows.

Then, if you delete 3 rows from the "item info" table, it's now got 4 rows
and the "comments" table needs to have 9-4=5 rows.

Assuming you always want to programmatically add/delete the last row(s) in
the "comments" table, and your tables are bookmarked "ItemInfo" and
"Comments", respectively, your code would go something like:

Sub MaintainTables()
Dim T1Rows As Integer
Dim T2Rows As Integer
Dim RowCount As Integer
Dim DefRows As Integer
DefRows = 9
With ActiveDocument
T1Rows = .Bookmarks("ItemInfo").Range.Tables(1).Rows.Count
T2Rows = .Bookmarks("Comments").Range.Tables(1).Rows.Count
RowCount = T1Rows + T2Rows
With .Bookmarks("Comments").Range.Tables(1)
If RowCount < DefRows Then
.Rows(T2Rows).Select
Selection.InsertRowsBelow DefRows - RowCount
ElseIf RowCount > ReqCount Then
For i = RowCount - T1Rows To DefRows - T1Rows + 1 Step -1
.Rows(i).Delete
Next
End If
End With
End With
End Sub

To work with a different number of rows, change the '9' in 'DefRows = 9' to
the required number.

Cheers
 
S

sam

HI, Macropod

Thank you!!! perfect!

I encountered a new problem right now. In this method, I think we
defaulted that the size of each row in both table is the same. However,
in my case, it is not like this. I only have one row in the table
"Comments". So the size of this specific row is variable. Here is my
idea, I want to set the ending point of the table "comments" in a
specific place of that page, like "row 21" of that page. Is it possible
to implement this using VBA? Do you have some good ideas? Thank you in
advance!!!

Best Regards

Sam


As the size of specific row in table is variable, for example, if I
deleted two rows in table "item info", which
 
M

macropod

Hi Sam,

If you want to adjust the height of your comment row, then you'll need to
set the height of the rows in the "item info" table to 'exact'. That's
because Word doesn't hold a meaningful value for row heights that are set to
autosize.

Other than that, the logic is pretty much the same but, instead of counting
rows, you'll be measuring row heights and adjusting the row height in your
"comments" table to suit.

Cheers
 
S

sam

HI,Macropod

As my understanding, your idea is to calculate the rows height that
added or deleted in table "item info", insteand of counting rows. Did
you know the clause in VBA to enlarge the height of only row in table
"comments"? Could you give me some hint?Thank you!

Best Regards

Sam
 
M

macropod

Hi Sam,

You don't need code to increase/decrease the row height as such - all you
need to do is to calculate what it should be and set it to that. Hint:
Tables(1).Rows(1).Height = ???

Cheers
 

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