vbTab \ Chr(9) Writing New Line in Word VBA

1.2k Views Asked by At

I am attempting to convert Word tables into paragraph text via VBA. I know about Convert to Text option for tables, but this is not working as my Word files are converted PDF files that have images which eventually will get processed through Python script. The tables have no reliable and consistent use of tab, para, or anything else when converted.

My code follows:

Option Explicit

Sub ReWriteTableData()

    Dim tb As Table

    For Each tb In ThisDocument.Tables

        tb.Select

        With Selection

            Dim rw As row, cl As Column
            Dim i As Integer

            .MoveUp Unit:=wdLine, Count:=1

            For Each rw In tb.Rows

                .EndKey Unit:=wdLine
                .TypeParagraph

                For i = 1 To tb.Columns.Count

                    Dim myText As String, myString As String
                    myString = rw.Cells(i).Range.Text
                    myString = Mid(myString, 1, Len(myString) - 1)

                    .TypeText Text:=myString & Chr(9)

                Next

            Next

        End With

    Next

End Sub

The place where I am stuck is .TypeText Text:=myString & Chr(9).

When that prints the text to the line above the table it places the 2nd - nth column on a new line, like this:


enter image description here


What I want is this:

enter image description here

I know pictures are slightly incomplete, but the loop isn't my issue, it's getting them all on one line. Also, I am sure my code is not the most efficient, so am happy to have any refactor to make it faster, easier, better to maintain.

0

There are 0 best solutions below