Import Word table into excel with line breaks

KeepOnWheels

New Member
Joined
May 14, 2015
Messages
7
Hello Guys!

This is my first post here. Glad to be here.


I'm trying to import tables from Word file to my excel sheet with VBA macro:

Code:
[COLOR=#405A04][FONT=Helvetica Neue]With wdDoc
    TableNo = wdDoc.tables.Count
    If TableNo = 0 Then
        MsgBox "This document contains no tables", _
        vbExclamation, "Import Word Table"
        Exit Sub
    'ElseIf TableNo > 1 Then
    '    TableNo = InputBox("This Word document contains " & TableNo & " tables." & vbCrLf & _
    '    "Enter table number of table to import", "Import Word Table", "1")
    End If
    
    
    For gettable = 1 To TableNo
    
        finalrow = Cells(Rows.Count, 1).End(xlUp).Row
        
        With .tables(gettable)
            'copy cell contents from Word table cells to Excel cells
            For iRow = 2 To .Rows.Count
                For iCol = 1 To .Columns.Count
                    Cells(iRow + finalrow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
                Next iCol
                Application.StatusBar = "Importing table no. " & gettable & " / " & TableNo & ", line: " & iRow & " / " & .Rows.Count
            Next iRow
        End With
    Next gettable
End With
[/FONT][/COLOR]


My problem is that in the word table line breaks are used. Something like this:

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Name if client: Hello
987888 Street Canada[/TD]
[TD]Customer No. 8855
Type: None[/TD]
[/TR]
[TR]
[TD]Name if client: The second
985888 Toronto[/TD]
[TD]Customer No. 8889
Type: None[/TD]
[/TR]
[TR]
[TD]Name if client: The third customer
98848788 Berlington[/TD]
[TD]Customer No. 7785
Type: Something[/TD]
[/TR]
</tbody>[/TABLE]

When I'm importing the data with the above method, the line break information get disappeared. I need to know where is the data separated because I need to past them to separate column.
Some of the information I can separate by finding different words and creating some rules but there are some data where no rule can be established.

How would you solve this?


Thanks for all your help!
 
Oh yeah, I'm dumb, It did not come into my mind to paste it to word as module.
The character is 13.

So did you try replacing Chr(11) with Chr(13)?

Strange is, that if I comment out the Exit sub and let run trough the whole cell I get this result: 13 than 49, 32, 52, 51, 52

Those are just the codes for subsequent characters.
 
Upvote 0

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
That's probably because one or more cells in the table do not contain line breaks. I should have made allowances for it. I'll do so now. However, just to be clear, is this the result you expect?

Name if client: Hello987888 Street CanadaCustomer No. 8855Type: None
Name if client: The second985888 TorontoCustomer No. 8889Type: None
Name if client: The third customer98848788 BerlingtonCustomer No. 7785Type: Something

<tbody>
</tbody>

If not, can you please clarify? If so, first add the following declaration...

Code:
Dim sa As Variant

Then try...

Code:
            [color=darkblue]For[/color] iRow = 2 [color=darkblue]To[/color] .Rows.Count
                [color=darkblue]For[/color] iCol = 1 [color=darkblue]To[/color] .Columns.Count
                    sa = Split(.cell(iRow, iCol).Range.Text, Chr(11))
                    Cells(iRow + finalrow, iCol * 2 - 2 + 1).Value = WorksheetFunction.Clean(Trim(sa(0)))
                    [color=darkblue]If[/color] [color=darkblue]UBound[/color](sa) > 0 [color=darkblue]Then[/color]
                        Cells(iRow + finalrow, iCol * 2).Value = WorksheetFunction.Clean(Split(.cell(iRow, iCol).Range.Text, Chr(11))(1))
                    [color=darkblue]End[/color] [color=darkblue]If[/color]
                [color=darkblue]Next[/color] iCol
                Application.StatusBar = "Importing table no. " & gettable & " / " & TableNo & ", line: " & iRow & " / " & .Rows.Count
            [color=darkblue]Next[/color] iRow
What if there's another row to split?
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top