Macro to make table fit on one page

P

Peter

Hello,

I wrote a macro that uses the Information property with wdActiveEndPageNumber in a loop to change the height of rows until the page number of the first row matches the number of the last row. After only about 6 loops, I get an error that word has run out of memory if I run it in the debugger but no error otherwise. In either case, it does not work. I don't know if this is a memory leak in the Information routine.
If anyone can suggest a better way to set row heights such that the entire table fits on one page, I sure would like to know.

Thanks,
Peter
 
M

macropod

Hi Peter,

A better way to go about this might be to determine the available text area
height (=page height - top margin - bottom margin) and divide that by the
count of rows for the table. That would give you the average row height
required to fit the table onto one page.

Something along the lines of the following (not tested) might work. It's
meant to go through all tables in a document, fitting them to one page high
for the page dimensions of whatever section they're in:

Sub TableFit()
Application.ScreenUpdating = False
Dim oTopMargin As Single, oBottomMargin As Single
Dim oPageHeight As Single, oPrintHeight As Single, oRowHeight As Single
Dim oTable, i As Integer, j As Integer
If ActiveDocument.Tables.Count = 0 Then Exit Sub
With ActiveDocument.PageSetup
oTopMargin = .TopMargin
oBottomMargin = .BottomMargin
oPageHeight = .PageHeight
End With
oPrintHeight = oPageHeight - oTopMargin - oBottomMargin
Restart:
For j = 1 To ActiveDocument.Tables.Count
oTable = ActiveDocument.Tables(j)
oRowHeight = int(oPrintHeight / oTable.Rows.Count)
For i = 1 To oTable.Rows.Count
oTable.Rows(i).Height = oRowHeight
Next
Next
Application.ScreenUpdating = True
End Sub

Cheers


Peter said:
Hello,

I wrote a macro that uses the Information property with
wdActiveEndPageNumber in a loop to change the height of rows until the page
number of the first row matches the number of the last row. After only about
6 loops, I get an error that word has run out of memory if I run it in the
debugger but no error otherwise. In either case, it does not work. I don't
know if this is a memory leak in the Information routine.
If anyone can suggest a better way to set row heights such that the entire
table fits on one page, I sure would like to know.
 
M

macropod

Hi again,

Updated (tested) macro below.

The macro fits all tables in a document to the height of the page in the
section in which they appear. Whether the tables actually print that way
depends on whether there is anything else on the same pages.

Sub TableFit()
Application.ScreenUpdating = False
Dim oTopMargin As Single, oBottomMargin As Single
Dim oPageHeight As Single, oPrintHeight As Single, oRowHeight As Single
Dim oTable, i As Integer
If ActiveDocument.Tables.Count = 0 Then Exit Sub
For i = 1 To ActiveDocument.Tables.Count
oTable = ActiveDocument.Tables(i)
With oTable.PageSetup
oTopMargin = .TopMargin
oBottomMargin = .BottomMargin
oPageHeight = .PageHeight
End With
oPrintHeight = oPageHeight - oTopMargin - oBottomMargin
oRowHeight = Int((oPrintHeight - 1) / oTable.Rows.Count)
With oTable.Rows
.Height = oRowHeight
.HeightRule = wdRowHeightExactly
End With
Next
Application.ScreenUpdating = True
End Sub

Cheers


Peter said:
Hello,

I wrote a macro that uses the Information property with
wdActiveEndPageNumber in a loop to change the height of rows until the page
number of the first row matches the number of the last row. After only about
6 loops, I get an error that word has run out of memory if I run it in the
debugger but no error otherwise. In either case, it does not work. I don't
know if this is a memory leak in the Information routine.
If anyone can suggest a better way to set row heights such that the entire
table fits on one page, I sure would like to know.
 
P

Peter

Thanks for your help!

macropod said:
Hi again,

Updated (tested) macro below.

The macro fits all tables in a document to the height of the page in the
section in which they appear. Whether the tables actually print that way
depends on whether there is anything else on the same pages.

Sub TableFit()
Application.ScreenUpdating = False
Dim oTopMargin As Single, oBottomMargin As Single
Dim oPageHeight As Single, oPrintHeight As Single, oRowHeight As Single
Dim oTable, i As Integer
If ActiveDocument.Tables.Count = 0 Then Exit Sub
For i = 1 To ActiveDocument.Tables.Count
oTable = ActiveDocument.Tables(i)
With oTable.PageSetup
oTopMargin = .TopMargin
oBottomMargin = .BottomMargin
oPageHeight = .PageHeight
End With
oPrintHeight = oPageHeight - oTopMargin - oBottomMargin
oRowHeight = Int((oPrintHeight - 1) / oTable.Rows.Count)
With oTable.Rows
.Height = oRowHeight
.HeightRule = wdRowHeightExactly
End With
Next
Application.ScreenUpdating = True
End Sub

Cheers



wdActiveEndPageNumber in a loop to change the height of rows until the page
number of the first row matches the number of the last row. After only about
6 loops, I get an error that word has run out of memory if I run it in the
debugger but no error otherwise. In either case, it does not work. I don't
know if this is a memory leak in the Information routine.
table fits on one page, I sure would like to know.
 

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