PDA

View Full Version : Solved: Questions About VBA Created Tables



MWE
02-14-2006, 10:35 PM
1. I am creating a table using VBA. I wish to horizontally center the text in a given cell. After poking around in the object browser and recording a macro for information purposes, it appears that this is achieved using the ParagraphFormat property of the cell's Range. For example:

Selection.Tables(1).Cell(Row:=2, Column:=1).Range.ParagraphFormat = wdAlignParagraphCenter

This statement generates a "Type Mismatch" compiler error.

So, clearly there is something wrong. What should I be doing?

2. I wish the first two rows in the table to be the "Header" and be repeated at the top of each page. It is pretty clear how to make one row the header, but how do I make two rows the header?


3. After the table is created (2 cols wide by N row long), I adjust the col widths to what I want, merge the cells in the first row and add text and formatting to row 1 and row 2


With Selection.Tables(1)
.Columns(1).PreferredWidthType = wdPreferredWidthPoints
.Columns(1).PreferredWidth = InchesToPoints(1.7)
.Columns(2).PreferredWidthType = wdPreferredWidthPoints
.Columns(2).PreferredWidth = InchesToPoints(6.7)
.cell(Row:=1, Column:=1).Merge MergeTo:=.cell(Row:=1, Column:=2)
.Rows(1).Range = "Table of Stuff"
.Rows(1).Range.Font.Size = 12
.Rows(1).Range.Bold = True
.Rows(1).Alignment = wdAlignRowCenter
.cell(Row:=2, Column:=1).Range = "AAAA"
.cell(Row:=2, Column:=1).Range.Bold = True
.cell(Row:=2, Column:=2).Range = "BBBB"
.cell(Row:=2, Column:=2).Range.Bold = True
.Rows(1).HeadingFormat = True
End With


The results are not quite what I want:

col 2 ends up being about 3.5 inches wide
the first row is offset from the rest of the table about 1.5" to the right
Any suggestions?

Thanks