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