'MS Word VBA - Centering a Table Needs Manual Refresh: Why and How to Fix?
This code inserts a table and tries to center it:
Sub Macro1()
wtrows = 4
wtcols = 3
Set tbl = ActiveDocument.Tables.Add(Selection.Range, wtrows, wtcols)
tbl.Rows.Alignment = wdAlignRowCenter '' centre entire table
For i = 1 To wtrows
For j = 1 To wtcols
tbl.Rows(i).Cells(j).Range.Text = 999
tbl.Columns(j).Cells.Width = CentimetersToPoints(2)
Next
Next
End Sub
However, the table appears as left-aligned. If I select the table then Ribbon | Home | Paragraph has the Centered toggle button selected (which does not match the appearance). If I force a manual refresh by Control Home/End or toggling Show/Hide Paragraph Marks at Ribbon | Home then the table now appears correctly as centered.
I've tried by code to provoke a full refresh (eg adjust a margin) but nothing works. How can I force the display to match the table properties? And any advice on why this is happening?
Word version is 365 MSO (Version 2204 Build 16.0.15128.20210) 64-bit, running on latest Win10.
Thanks.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
