none
Flip all tables' reading direction in a document, works inconclusively. RRS feed

  • Question

  • Hi,

    Below is the macro I use to flip tables throughout the doc, but sometimes it doesn't work well.

    Sub tableFlip()
    Dim tbl As Table
    For Each tbl In ActiveDocument.Tables
        If tbl.TableDirection = wdTableDirectionLtr Then tbl.TableDirection = wdTableDirectionRtl Else _
        tbl.TableDirection = wdTableDirectionLtr
    Next
    End Sub

    I have the macro above called when the first option ('flip tables') in the Userform is selected. There are two other options, the second one forces all tables to right-to-left ('tables RTL') and the third forces all to left-to-right ('tables LTR'). These two options seem to work well 100% of the time.

    But the first option works well only MOST of the time:
    If I select the first option ('flip tables') when the userform is first loaded and hit Run, then this works as expected (flips each table's reading direction).
    But, if I select any of the other two options, hit Run, and then select the first one (without unloading the userform) - this time it fails to flip all of the tables (seems to alternatively skip tables). The result is that some tables did not get flipped. When I hit Go again (run the same flip a second time without changing anything), it works well again, as expected.

    Any idea why the flip macro acts up so unexpectedly? What do I need to change in the macro to make it work 100% of the time?

    Much appreciated!
    :)

    Saturday, July 27, 2019 1:19 PM

All replies

  • If unloading is the problem, you can add a code to unload and load it back again at the end of tableFlip Sub. This should reset the user-form.

    Sub tableFlip() Dim tbl As Table For Each tbl In ActiveDocument.Tables If tbl.TableDirection = wdTableDirectionLtr Then tbl.TableDirection = wdTableDirectionRtl Else _ tbl.TableDirection = wdTableDirectionLtr Next

    Unload Me

    UserForm.Show End Sub

    Saturday, July 27, 2019 5:55 PM
  • Hey,

    This doesn't get rid of the glitch...I wonder what makes it do that.
    Maybe another underlying setting needs reset on the doc level... I got nothin...

    Thanks anyway though :)



    • Edited by GGLK Saturday, July 27, 2019 10:05 PM
    Saturday, July 27, 2019 9:48 PM
  • It seems that tbl.TableDirection does not always report the real value.

    Try an experimental workaround:

    Sub tableFlip()
     
        Dim tbl As Table
        
        For Each tbl In ActiveDocument.Tables
        
            If GetTableDirection(tbl) = wdTableDirectionLtr Then
                tbl.TableDirection = wdTableDirectionRtl
            Else
                tbl.TableDirection = wdTableDirectionLtr
            End If
        
        Next
        
    End Sub
     
     
    Function GetTableDirection(ByVal tbl As Table) As WdTableDirection
     
        GetTableDirection = IIf(InStr(1, tbl.Range.WordOpenXML, "<w:bidiVisual/>") > 0, wdTableDirectionRtl, wdTableDirectionLtr)
     
    End Function
    

     

    If this alternative works, it can be improved using a more elaborated XML parsing.

     

    Monday, July 29, 2019 3:30 PM