Extract Table from Word After Locating the Non-Table Content Above That Table VBA

ChrisOK

Well-known Member
Joined
Mar 26, 2003
Messages
601
I've got code that will extract all Tables from a Word doc.. fantastic!
I've got code that will extract all NON-table content from a Word doc... also fantastic!
I've got code that will use a msg box to go seek out a word or phrase and then extract in the whole row.. again great!

But now I've encountered a scenario where a Table within a Word doc needs to be extracted -- however, it's Table Name is in regular verbiage (above) and outside the table... Go figure... Grrrr ...

I'm hoping there's a way to combine these 2 sets of code that work beautifully to be able to do this:
1=Enter the word/phrase I want sought out ---
2=Once a 'hit' is found -- it will copy/paste/extract that table into a specified Worksheet called: "RESULTS" within the 'Active Workbook' that I would have open --- that would initiate the code..

HERE'S THE CODE THAT USES A SEARCH FEATURE:
Code:
Public Sub FindMultipleSheet****s()
'
'loops through all the sheets within a workbook (except the one you tell it NOT to touch) because
'that one is reserved for pasting findings to it...
'
'Run from standard module, like: Module1.
 'Find all data on all sheets!
 'Do not search the sheet the found data is copied to!
 'List a message box with all the found data addresses, as well!
 Dim ws As Worksheet, Found As Range
 Dim myText As String, firstAddress As String
 Dim AddressStr As String, foundNum As Integer

 myText = InputBox("Enter text to find")

If myText = "" Then Exit Sub

For Each ws In ThisWorkbook.Worksheets
With ws
'Do not search sheet25!=== ENTER NAME OF SHEET THAT YOU DO NOT WANT SEARCHED===
If ws.Name = "MASTER" Then GoTo myNext

Set Found = .UsedRange.Find(what:=myText, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False)

If Not Found Is Nothing Then
firstAddress = Found.Address

Do
foundNum = foundNum + 1
 AddressStr = AddressStr & .Name & " " & Found.Address & vbCrLf

Set Found = .UsedRange.FindNext(Found)

'Copy found data row to sheet4 Option! === ENTER NAME OF SHEET WHERE YOU WANT RESULTS PLACED===
'IT WILL LOCATE THE UNIQUE WORD OR PHRASE AND EXTRACT EVERYTHING ON THE WHOLE ROW
'this is very helpful to be able to see if this is valid content wanted or not
'IT ALSO WILL PROVIDE A MSG BOX SHOWING EXACT LOCATION OF ALL HITS
 Found.EntireRow.Copy _
 Destination:=Worksheets("RESULTS").Range("A65536").End(xlUp).Offset(1, 0)
Loop While Not Found Is Nothing And Found.Address <> firstAddress
End If

myNext:
End With

Next ws

If Len(AddressStr) Then
MsgBox "Found: """ & myText & """ " & foundNum & " times." & vbCr & _
 AddressStr, vbOKOnly, myText & " found in these cells"
 Else:

 MsgBox "Unable to find " & myText & " in this workbook.", vbExclamation
End If
End Sub

HERE'S THE CODE THAT EXTRACTS ALL TABLES OUT OF A WORD DOC (.DOC or .DOCX) -ability to edit in code as needed:
(I only want the table that's found immediately beneath the search phrase -- in this case, not ALL tables)
Code:
Sub ImportWordTableDOC()

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

On Error Resume Next

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

If wdFileName = False Then Exit Sub '(user cancelled import file browser)
  Set wkSht = ActiveSheet
  wkSht.Range("A:AZ").ClearContents

  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
            wkSht.Cells(resultRow, iCol) = WorksheetFunction.Clean(.Cell(iRow, iCol).Range.Text)
          Next iCol
          resultRow = resultRow + 1
        Next iRow
      End With
      resultRow = resultRow + 1
      With wkSht
        .Range(.Cells(resultRow, 1), .Cells(resultRow, iCol)).Interior.ColorIndex = 15
      End With
      resultRow = resultRow + 1
    Next tableStart
End With

End Sub

In advance, thank you - I know there's a brilliant mind out there that knows how to combine something like this..
 
Here's what happens when I try to run it on the .doc files:
(but it never has any probs with the .docx files)
The code crashes at this point -- and the bolded line below is the one that's highlighted in yellow...
tn = Cells(Evaluate("=match(vlookup(" & spos & ",a1:b" & lr & ",2,true),b1:b" & lr & ",0)+1"), 2)

With error: "Run-time error 13 - Type mismatch"

Code:
Next
Sorter Sheets("Sheet2"), [a1].CurrentRegion                             ' sort table information
lr = CStr(Range("a" & Rows.Count).End(xlUp).Row)
tn = Cells(Evaluate("=match(vlookup(" & spos & ",a1:b" & lr & ",2,true),b1:b" & lr & ",0)+1"), 2)
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Problem:
The code works w/ DOCX files but won't work w/ .DOC files, is there a way to adjust it to make it work on both ---or at least have the ability to switch it back and forth within the code when working with large batches of each file type?

I didn't see any refcs of file types within the code, so I'm not sure where it would be adjusted?

I'm getting a Run time error 13 - Type Mismatch ---and the following line is what's highlighted in yellow - which led me to believe maybe changing a 1,2 setting might toggle it between working on docx vs doc files?
Code:
tn = Cells(Evaluate("=match(vlookup(" & spos & ",a1:b" & lr & ",2,true),b1:b" & lr & ",0)+1"), 2)

Hope someone knows how to resolve this problem?
 
Upvote 0
I am not sure the problem is the file format.
When the yellow line appears, please place the mouse over the variables “spos” and “lr”, this will show their values. Post back the values so I can analyze them.
 
Upvote 0
This version:

- Checks if the desired string is present at the Word document
- Eliminates an unnecessary loop

Code:
Sub FindTable()
Dim i%, j%, ow, mydoc As Document, mytext$, spos, lr$, tn%, wr As Word.Range
Set ow = GetObject(, "Word.Application")
ow.Visible = True
Set mydoc = ow.ActiveDocument
mytext = InputBox("Enter text to find")
Set wr = mydoc.Content
wr.Find.Execute mytext, , , , , , True
If wr.Find.Found = False Or mytext = "" Then
    MsgBox "Text not found!", vbCritical, mytext
    Exit Sub
End If
ow.Selection.HomeKey wdStory
ow.Selection.Find.ClearFormatting
With ow.Selection.Find
    .ClearFormatting
    .Text = mytext
    .Forward = True
    .Wrap = wdFindStop
    .Execute                                                            ' find string
End With
ow.Selection.HomeKey wdLine
spos = CStr(ow.Selection.Information(wdActiveEndPageNumber) + _
(ow.Selection.Information(wdFirstCharacterLineNumber) / 100))           ' string position index
spos = Replace(spos, ",", ".")
Sheets("sheet4").Activate
[a:b].ClearContents
For i = 1 To mydoc.Tables.Count
    mydoc.Tables(i).cell(1, 1).Select
    Cells(i, 1) = ow.Selection.Information(3) + _
    (ow.Selection.Information(10) / 100)                                ' table position index
    Cells(i, 2) = i                                                     ' table number
Next
Sorter Sheets("sheet4"), [a1].CurrentRegion                             ' sort table information
lr = CStr(Range("a" & Rows.Count).End(xlUp).Row)
tn = Cells(Evaluate("=match(vlookup(" & spos & ",a1:b" & lr & ",2,true),b1:b" & lr & ",0)+1"), 2)
mydoc.Tables(tn).Range.Copy
Sheets("results").Paste Destination:=Sheets("results").[a1]
Set mydoc = Nothing
Set ow = Nothing
MsgBox "end of code"
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,104
Messages
6,128,856
Members
449,472
Latest member
ebc9

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