Skip to content

Instantly share code, notes, and snippets.

@jvarn
Last active March 26, 2024 09:54

Revisions

  1. jvarn revised this gist Mar 26, 2024. 1 changed file with 32 additions and 15 deletions.
    47 changes: 32 additions & 15 deletions reverse-table-columns.vb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    ' Tested with simple table (no merged cells) on:
    ' Microsoft® Word for Mac
    ' Version 16.83
    ' Microsoft 365
    ' Reverses LTR tables with visually RTL content and vice versa
    ' Tested with simple table (no merged cells) on Microsoft 365 versions of:
    ' Microsoft Word for Mac, Version 16.83
    ' Microsoft Word for Windows, Version 2402
    Sub ReverseTableColumns()
    Dim tbl As Table
    Dim colCount As Long
    @@ -13,24 +13,41 @@ Sub ReverseTableColumns()
    Set tbl = Selection.Tables(1)
    colCount = tbl.Columns.Count

    ' Loop through half the columns, swapping the first and last, moving towards the middle.
    ' Loop through cols, swap 1st & last, moving towards middle.
    For i = 1 To colCount / 2
    For j = 1 To tbl.Rows.Count
    ' Store first column content in a temporary variable
    tempContent = tbl.Cell(j, i).Range.Text
    ' Move last column content to first
    tbl.Cell(j, i).Range.Text = tbl.Cell(j, colCount - i + 1).Range.Text
    ' Move temporary content (original first column) to last
    tbl.Cell(j, colCount - i + 1).Range.Text = tempContent
    ' Store first column content in temp variable
    tempContent = tbl.cell(j, i).Range.Text
    ' Move last column content to 1st
    tbl.cell(j, i).Range.Text = tbl.cell(j, colCount - i + 1).Range.Text
    ' Move temp content (original 1st column) to last
    tbl.cell(j, colCount - i + 1).Range.Text = tempContent
    Next j
    Next i

    ' Adjust table properties for RTL layout
    ' Adjust table properties.
    With tbl
    .Rows.Alignment = wdAlignRowRight
    ' Change the reading order of the table
    .Range.ParagraphFormat.ReadingOrder = wdReadingOrderRtl
    If .Rows.TableDirection = wdTableDirectionLtr Then
    .Rows.TableDirection = wdTableDirectionRtl
    .Rows.Alignment = wdAlignRowRight
    .Range.ParagraphFormat.ReadingOrder = wdReadingOrderRtl
    Elseif .Rows.TableDirection = wdTableDirectionRtl Then
    .Rows.TableDirection = wdTableDirectionLtr
    .Rows.Alignment = wdAlignRowLeft
    .Range.ParagraphFormat.ReadingOrder = wdReadingOrderLtr
    End If
    End With

    ' Remove trailing paragraph marks.
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = "^p"
    .Replacement.Text = ""
    .Forward = True
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

    Else
    MsgBox "Please select a table first.", vbInformation
    End If
  2. jvarn created this gist Mar 25, 2024.
    37 changes: 37 additions & 0 deletions reverse-table-columns.vb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    ' Tested with simple table (no merged cells) on:
    ' Microsoft® Word for Mac
    ' Version 16.83
    ' Microsoft 365
    Sub ReverseTableColumns()
    Dim tbl As Table
    Dim colCount As Long
    Dim i As Long
    Dim j As Long
    Dim tempContent As String

    If Selection.Information(wdWithInTable) Then
    Set tbl = Selection.Tables(1)
    colCount = tbl.Columns.Count

    ' Loop through half the columns, swapping the first and last, moving towards the middle.
    For i = 1 To colCount / 2
    For j = 1 To tbl.Rows.Count
    ' Store first column content in a temporary variable
    tempContent = tbl.Cell(j, i).Range.Text
    ' Move last column content to first
    tbl.Cell(j, i).Range.Text = tbl.Cell(j, colCount - i + 1).Range.Text
    ' Move temporary content (original first column) to last
    tbl.Cell(j, colCount - i + 1).Range.Text = tempContent
    Next j
    Next i

    ' Adjust table properties for RTL layout
    With tbl
    .Rows.Alignment = wdAlignRowRight
    ' Change the reading order of the table
    .Range.ParagraphFormat.ReadingOrder = wdReadingOrderRtl
    End With
    Else
    MsgBox "Please select a table first.", vbInformation
    End If
    End Sub