2016-11-15 29 views
1

我正在嘗試創建一個表並垂直居中文本。當我創建表格時,我使用文本填充單元格並應用對齊方式:發佈者VBA垂直文本對齊屬性不適用

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 

但是,文本不能正確垂直對齊。當我查看「格式表」下的「單元格屬性」選項卡時,我發現它已設置爲「中間」垂直對齊方式,但只有當我點擊「確定」時纔會應用此選項(如果我點擊取消,則沒有任何更改)。

如果在應用對齊之前或之後更改文本,則無關緊要。任何人有任何想法,爲什麼發生這種情況?

+0

您是否在尋找比「看起來像一個bug」更具洞察力的東西? ;) – Jbjstam

+0

你需要找出一種方法來首先獲得正確的Hwnd(.ActiveWindow可以引用VBE編輯器),但是你可以使用SetForegroundWindow和SendKeys「%JL1」,True – Jbjstam

回答

0

看起來像你發現了一個錯誤。這是一個解決方法的嘗試..

#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