centering text in cell using vba

R

ryan_eng

Hi all,

I am writing some vba code to insert a table at a specific bookmark. So far
I have succeeded at doing the following:
-insert table at bookmark with 4 rows, 4 columns.
-set autoformat with a small variation to header row
-merge cells in row 2 and insert test

Now I want to center the text in the merged cell of row 2 but I can't seem
to find the command on the internet. Help!
My code so far is below:

Sub CreateNewTable()
Dim docActive As Document
Dim tblNew As Table
Dim celTable As Cell

Set docActive = ActiveDocument
Set tblNew = docActive.Tables.Add( _
Range:=docActive.Bookmarks("table1").Range, NumRows:=3, _
NumColumns:=4)

With ActiveDocument.Tables(1)
.Cell(Row:=1, Column:=1).Range.InsertAfter Text:="Document No."
.Cell(Row:=1, Column:=2).Range.InsertAfter Text:="Rev."
.Cell(Row:=1, Column:=3).Range.InsertAfter Text:="Date"
.Cell(Row:=1, Column:=4).Range.InsertAfter Text:="Description"
.Cell(Row:=2, Column:=1).Range.InsertAfter Text:="Installation
Documents"
End With

tblNew.AutoFormat Format:=wdTableFormatGrid1, _
ApplyBorders:=True, ApplyFont:=True, ApplyColor:=True
With ActiveDocument.Tables(1)
.Rows(1).Range.Font.Bold = True
.Rows(2).Cells.Merge
End With

End Sub
 
K

Karl E. Peterson

ryan_eng said:
Hi all,

I am writing some vba code to insert a table at a specific bookmark.
So far I have succeeded at doing the following:
-insert table at bookmark with 4 rows, 4 columns.
-set autoformat with a small variation to header row
-merge cells in row 2 and insert test

Now I want to center the text in the merged cell of row 2 but I can't
seem to find the command on the internet. Help!

Wouldn't something like .HorizontalAlignment = xlCenter be the ticket?
 
R

ryan_eng

How would I execute that? The only way I could access the HorizontalAlignment
command was by putting .Rows.HorizontalAlignment = xlcenter, but that doesn't
specify a row.
 

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