Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Center align cell text in MSWord 2000 table using VBA ?

Status
Not open for further replies.

vamoose

Programmer
Oct 16, 2005
320
MX
Trying to center align text within this cell in a Word 2000 table using VBA.

I have tried with no success:

.cell(2, 2).Range.Font.Alignment = wdAlignCenter
.cell(2, 2).Range.Font.Alignment = wdnCenter
.cell(2, 2).Range.Font.HorizontalAlignment = wdAlignCenter

Any suggestions, please ? Thanks
 
It is not an attribute of FONT, it is an attribute of the paragraph. Fonts are not centered.
Code:
ActiveDocument.Tables(1).Cell(2,2) _
    .Range.ParagraphFormat.Alignment = _
    wdAlignParagraphCenter

Gerry
 
Code:
.cell(2, 2).Shape..TextFrame.TextRange.ParagraphFormat.Alignment = wdAlignParagraphCenter

cant use word at the moment, but something like the above

Chance,

Filmmaker, taken gentleman and He tan e epi tas
 
Hey Chance. Shape? TextFrame? Ai carumba! Shudder....no need to mess around with Shape. As far as we know there IS no Shape. It is a cell in a table. Just using Range works fine.

Gerry
 
#Ah been in the bowels of powerpoint all week-,

Chance,

Filmmaker, taken gentleman and He tan e epi tas
 
I have tried this but for some reason it in not working, any ideas why ? Thanks.

Set tblWord = docWord.Tables.Add(docWord.Range(0), numrows:=46, numcolumns:=12): With tblWord

.Cell(2, 2).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
 
needs tidying up but

Code:
Sub Green()


Set myRange = ActiveDocument.Range(Start:=0, End:=0)
ActiveDocument.Tables.Add Range:=myRange, NumRows:=46, NumColumns:=12
ActiveDocument.Tables(1).Cell(1, 1).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter

End Sub

Chance,

Filmmaker, taken gentleman and He tan e epi tas
 
vamoose, could you specify what "not working" means? It seems you are not working in Word, as you have the docWord, but the following works fine for me.
Code:
Dim tblWord As Word.Table
Set tblWord = ActiveDocument.Tables.Add _
   (Range:=Selection.Range, numrows:=46, numcolumns:=12)
  With tblWord
    .Cell(2, 2).Range.ParagraphFormat.Alignment = _
         wdAlignParagraphCenter
  End With
Set tblWord = Nothing
Now, true I am using Range:=Selection.Range in the Tables.Add instruction. And you have docWord.Range(0). So I am wondering if that is the problem. It needs a Range:= parameter.

So what is "not working"?

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top