1

I'm trying to create a table and center the text within vertically. When I create the table, I fill the cell with text and apply the alignment:

Dim pg As Page
Dim tbl As Shape

Set pg = ActiveDocument.ActiveView.ActivePage

Set tbl = pg.Shapes.AddTable(1, 1, InchesToPoints(3), InchesToPoints(5), InchesToPoints(2), InchesToPoints(1))

With tbl.Table.Rows(1).Cells(1)
    .TextRange.Text = "Hello, World!"
    .VerticalTextAlignment = pbVerticalTextAlignmentCenter
End With

However, the text doesn't vertically align properly. When I look in the Cell Properties tab under "Format Table," I see it is set to "Middle" vertical alignment already, but will only apply this once I hit "OK" (If I hit cancel, nothing changes).

It doesn't matter if I change the text before or after the alignment is applied. Anyone have any ideas on why this is happening?

KBerstene
  • 73
  • 5
  • Are you looking for something more insightful than "looks like a bug"? ;) – Jbjstam Nov 22 '16 at 07:58
  • You'd need to figure out a way to obtain the correct Hwnd first (.ActiveWindow can refer to the VBE editor), but you could use SetForegroundWindow and SendKeys "%JL1", True – Jbjstam Nov 22 '16 at 09:31

1 Answers1

0

Seems like you found a bug. Here's an attempt at a workaround..

#If Win64 Then
    Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
#Else
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds as Long) 'For 32 Bit Systems
#End If

Sub Centralize(oCellRange As CellRange)
    oCellRange.Select
    Sleep 500
    Application.ActiveDocument.ActiveWindow.Activate
    SendKeys "%JL1", True
End Sub

Sub Test()
    Dim oCellRange As CellRange
    Set oCellRange = Application.ActiveDocument.Pages(1).Shapes(1).Table.Cells(1, 1, 2, 2)
    Call Centralize(oCellRange)
End Sub
Jbjstam
  • 874
  • 6
  • 13