user2791146
user2791146

Reputation: 39

How To Center Text in a table that is created using a macro in Word

I have a macro that creates a table with 2 columns. I want to center the text.

I need to know the actual function/method to do this (i.e not recorded) as I am editing a complex macro in a specific tool outside of Microsoft Word.

Function TableStyleApply(oTable)
  Const wdLineWidth050pt = 4
  Const wdLineStyleSingle = 1
  Const wdBorderTop = -1
  Const wdBorderLeft = -2
  Const wdBorderBottom = -3
  Const wdBorderRight = -4
  Const wdBorderHorizontal = -5
  Const wdBorderVertical = -6
  Const wdAlignParagraphCenter = 100

  oTable.Borders(wdBorderTop ).LineStyle = wdLineStyleSingle
  oTable.Borders(wdBorderLeft ).LineStyle = wdLineStyleSingle
  oTable.Borders(wdBorderBottom ).LineStyle = wdLineStyleSingle
  oTable.Borders(wdBorderRight ).LineStyle = wdLineStyleSingle
  oTable.Borders(wdBorderHorizontal).LineStyle = wdLineStyleSingle
  oTable.Borders(wdBorderVertical).LineStyle = wdLineStyleSingle

  oTable.Rows(1).Range.Font.Bold = True
  oTable.Rows(1).Shading.BackgroundPatternColor = 15132390
  oTable.Rows.LeftIndent = 43
  oTable.Columns(1).SetWidth 280, 2
  oTable.Columns(2).SetWidth 157, 2

  oTable.Columns.ParagraphFormat.Alignment = wdAlignParagraphCenter

End Function

Upvotes: 1

Views: 19226

Answers (2)

Jake Burdsall
Jake Burdsall

Reputation: 1

This line set alignment to center for all cells in a specific table:

cDoc.Tables(1).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter

Upvotes: 0

Kazimierz Jawor
Kazimierz Jawor

Reputation: 19067

You need to refer to any Range object if you want to align your text in center. So, try with this options

For whole table

oTable.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter

For any single column (here, for 1st and 2nd columns)

oTable.Columns(1).Select
    Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter

oTable.Columns(2).Select
    Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter

Upvotes: 4

Related Questions