Word VBA: Table Pagination Problem

dfenton21

Board Regular
Joined
Jun 23, 2007
Messages
135
I'm a VBA developer for about 3 years, and I've just started work on my first Word project. I much prefer the Excel object model, but that it is a different story. I know this is an Excel site, but hopefully someone here has encountered this issue before, so I'd appreciate your help.

Basically, I need to create a new document and place text at very specific locations. For numerous reasons, each block of text must be within a one cell table. Normally, when I am just manually typing a document and I need specific control of a tables's y location, I just set the font size of the previous paragragh to 1 and increase pixel by pixel as required.

I don't want to use such a messy approach for an automated document, so I've used the folllowing code to set a table's specific location:

Code:
Sub foo() 
    Dim tbl As Table 
    Set tbl = ActiveDocument.Tables.Add(ActiveDocument.Range, 1, 1) 
     
    tbl.Borders(wdBorderTop).LineStyle = wdLineStyleSingle 
    tbl.Borders(wdBorderBottom).LineStyle = wdLineStyleSingle 
    tbl.Borders(wdBorderLeft).LineStyle = wdLineStyleSingle 
    tbl.Borders(wdBorderRight).LineStyle = wdLineStyleSingle 
     
    tbl.Rows.VerticalPosition = 600 
    tbl.Rows(1).AllowBreakAcrossPages = False 
     
    tbl.Cell(1, 1).Range.Text = "Text Text Text Text Text" 
     
End Sub
Note that I've not allowed the row to break across pages, which is important for my requirements.

My problem is as follows:
If I manually insert a table, and do not allow it to break across pages, the entire table will move to the next page if the text takes up too much space to fit on the first page.(which is correct).
But when I insert the table using the above code, if the text takes up too much space, the table's height just increases instead of moving to the next page.

I believe this behaviour is related to the fact that by using the code to insert the table, it will not insert paragraph marks throughout the page.

Is there a way around this problem? Basically my VBA requirements are:

Insert a one cell table at a specific location on a document
Table will move to the next page if its text is too big to fit in the remaining space on the page
The final document will end up containing dozens of these one cell tables.
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Forum statistics

Threads
1,225,626
Messages
6,186,094
Members
453,337
Latest member
fiaz ahmad

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top