import tables from word - only specific columns

panlex

New Member
Joined
Apr 5, 2013
Messages
1
hello everyone! i hope you can help me with my problem.I have a word file with lots of tables in it and want to import it to excel but only import specific columns from each table.What is more, i want to rename some of the data if a specific conditions exists.

this a sample of the word file (the actual file is over 200 pages) :https://dl.dropbox.com/u/13471344/test.doc (the .doc is in Greek)

1. i want to insert only certain column of each table (based on the column heading)

Which means read the header of each column - of each table - and only insert certain columns from each table (the decider being the header name)

2. i want to rename the data of the column:

for example if column has the data:

[TABLE="width: 50"]
<tbody>[TR]
[TD]381a.1[/TD]
[/TR]
[TR]
[TD].2[/TD]
[/TR]
[TR]
[TD].3[/TD]
[/TR]
[TR]
[TD].4[/TD]
[/TR]
[TR]
[TD].5[/TD]
[/TR]
</tbody>[/TABLE]

i want to read the first cell and then rename the rest cells as:

[TABLE="width: 50"]
<tbody>[TR]
[TD]381a.1[/TD]
[/TR]
[TR]
[TD]381a.2[/TD]
[/TR]
[TR]
[TD]381a.3[/TD]
[/TR]
[TR]
[TD]381a.4[/TD]
[/TR]
[TR]
[TD]381a.5[/TD]
[/TR]
</tbody>[/TABLE]

I have managed to create the macro for inserting all the tables to excel (with the help of Google and my minor skills) but do not know how to do the rest ...

this is the module so far...

Code:
Option Explicit


Sub ImportWordTable()

Dim wdDoc As Object
Dim wdFileName As Variant
Dim tableNo As Integer 'table number in Word
Dim iRow As Long 'row index in Excel
Dim iCol As Integer 'column index in Excel
Dim resultRow As Long
Dim tableStart As Integer
Dim tableTot As Integer

On Error Resume Next

ActiveSheet.Range("A:AZ").ClearContents

wdFileName = Application.GetOpenFilename("Word files (*.doc),*.doc", , _
"Browse for file containing table to be imported")

If wdFileName = False Then Exit Sub '(user cancelled import file browser)

Set wdDoc = GetObject(wdFileName) 'open Word file

With wdDoc
    tableNo = wdDoc.tables.Count
    tableTot = wdDoc.tables.Count
    If tableNo = 0 Then
        MsgBox "This document contains no tables", _
        vbExclamation, "Import Word Table"
    ElseIf tableNo > 1 Then
        tableNo = InputBox("This Word document contains " & tableNo & " tables." & vbCrLf & _
        "Enter the table to start from", "Import Word Table", "1")
    End If

    resultRow = 4

    For tableStart = 1 To tableTot
        With .tables(tableStart)
            'copy cell contents from Word table cells to Excel cells
            For iRow = 1 To .Rows.Count
                For iCol = 1 To .Columns.Count
                    Cells(resultRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
                Next iCol
                resultRow = resultRow + 1
            Next iRow
        End With
        resultRow = resultRow + 1
    Next tableStart
End With

End Sub

<code><o:p> </o:p></code>
thank you in advance!
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

Forum statistics

Threads
1,222,398
Messages
6,165,767
Members
451,986
Latest member
samwize

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