Quantcast
Channel: Word for Developers forum
Viewing all articles
Browse latest Browse all 4350

Extracting Data from Word Table into Excel (2010 suite and VBA Macro)

$
0
0

Good afternoon and please excuse the interruption.

I am looking to do the following:

I have a 2000+ page Word document that contains a table. The table has 5 Columns (lets say they are named Column1-5 accordingly)

The goal is to export data from the word table and import it into an excel spreadsheet. Here is the twist though. I want the macro to look for a word in Column5, if the word Patent is in there, export that row into excel, if not, dont export it. It needs to scan the entire table and pull out the rows where the content in Column5 has the word Patent (it can be anywhere in that cell)

I came across a VBA code that when placed in Excel and run, will take the table created in word and export it all into excel, which is cool but I feel thats only 80% of what I need it to do.

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

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
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 table number of table to import", "Import Word Table", "1")
End If
With .tables(TableNo)
'copy cell contents from Word table cells to Excel cells
For iRow = 1 To .Rows.Count
For iCol = 1 To .Columns.Count
Cells(iRow, iCol) = WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
Next iCol
Next iRow
End With
End With

Set wdDoc = Nothing

End Sub

I need it to also prompt for any doc or docx file.

Can anyone assist?


Viewing all articles
Browse latest Browse all 4350

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>