VBA: Extract specific word table to excel

Xlacs

Board Regular
Joined
Mar 31, 2021
Messages
105
Office Version
  1. 2016
Platform
  1. Windows
Hi Good People,

I'm having a hard timing fixing the below code.
My problem is I wanted to extract a multiple survey answer in ms word table..
The below code extract only a single word file. I wanted to extract all the docx with file name Form* (e.g. Form 1,Form2, FOrm 3, etc.)

The word table looks like the below. I only wanted to extract the "x" starting from row 2 column 2..

Hoping someon can actually help me on this.. Thank you in Advance!

QuestionsAgreeDisagreeNeutralStrongly Disagree
What is ----x
What is -----x

VBA Code:
Sub extractData()

Dim wd As New Word.Application
Dim doc As Word.Document
Dim sh As Worksheet

wd.Visible = True

Set doc = wd.Documents.Open(ActiveWorkbook.Path & "\Form.docx")
Set tbls = doc.Tables
Set sh = ActiveSheet

lr = sh.Cells(Rows.Count, 1).End(xlUp).Row + 1

For i = 1 To 6
    sh.Cells(lr, i).Value = Application.WorksheetFunction.Clean(tbls(1).Rows(i).Cells(2).Range.Text)
Next

doc.Close
wd.Quit
Set doc = Nothing
Set sh = Nothing
Set wd = Nothing


End Sub
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Here's some untested , not well commented code, that looks like it should work for 1 doc with multiple tables. HTH. Dave
Code:
With Wd.Activedocument
tableTot = .tables.Count
For tableStart = 1 To tableTot
With .tables(tableStart)
For iRow = 2 To .Rows.Count ' table rows
ColCnt = 1 'sht column
lr = sh.Cells(Rows.Count, 1).End(xlUp).Row + 1 'sht row
For iCol = 1 To .Columns.Count 'table columns
If .cell(iRow, iCol).Range.Text = "X" Then
sh.Cells(lr, ColCnt).Value = Application.WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
ColCnt = ColCnt + 1
End If
Next iCol
Next iRow
End With
Next tableStart
End With
 
Upvote 0
Here's some untested , not well commented code, that looks like it should work for 1 doc with multiple tables. HTH. Dave
Code:
With Wd.Activedocument
tableTot = .tables.Count
For tableStart = 1 To tableTot
With .tables(tableStart)
For iRow = 2 To .Rows.Count ' table rows
ColCnt = 1 'sht column
lr = sh.Cells(Rows.Count, 1).End(xlUp).Row + 1 'sht row
For iCol = 1 To .Columns.Count 'table columns
If .cell(iRow, iCol).Range.Text = "X" Then
sh.Cells(lr, ColCnt).Value = Application.WorksheetFunction.Clean(.cell(iRow, iCol).Range.Text)
ColCnt = ColCnt + 1
End If
Next iCol
Next iRow
End With
Next tableStart
End With

Wooh! Thanks. However, I'm having an error with " the command is not available because no document is open"

VBA Code:
Sub Open_Multiple_Word_Files(sPattern As String)
Dim shApp As Object
Dim shFolder As Object
Dim File, Files
Dim currentPath As String


'get folder
    Set shApp = CreateObject("shell.application")
    folder2search = "C:\Users\ChrisLacs\Desktop\extrac\"
    Set shFolder = shApp.Namespace(folder2search)
'get files
    Set Files = shFolder.Items()
'check if file names match the pattern
    For Each File In Files
        If File.Name Like sPattern Then
        
        
           

Dim wd As New Word.Application
Dim doc As Word.Document
Dim sh As Worksheet

wd.Visible = True

With wd.ActiveDocument
tableTot = .Tables.Count
For tableStart = 1 To tableTot
With .Tables(tableStart)
For iRow = 2 To .Rows.Count ' table rows
ColCnt = 1 'sht column
lr = sh.Cells(Rows.Count, 1).End(xlUp).Row + 1 'sht row
For iCol = 1 To .Columns.Count 'table columns
If .Cell(iRow, iCol).Range.Text = "X" Then
sh.Cells(lr, ColCnt).Value = Application.WorksheetFunction.Clean(.Cell(iRow, iCol).Range.Text)
ColCnt = ColCnt + 1
End If
Next iCol
Next iRow
End With
Next tableStart
End With



            
            Debug.Print File.Name & " - " & (File.Name Like sPattern)
        
        End If
    Next File
End Sub

Sub Search_it()
    Open_Multiple_Word_Files "Form*"
End Sub
 
Upvote 0
You haven't opened any documents. You need to ...
Code:
Wd.Documents.Open Filename:=docPath
Where "docpath" is the full path to the doc U want to extract info from. Also, using "File" as a variable is not good as it has a specific XL meaning. Maybe this thread will help...
Dave
 
Upvote 0
Wooh! Thanks. However, I'm having an error with " the command is not available because no document is open"

VBA Code:
Sub Open_Multiple_Word_Files(sPattern As String)
Dim shApp As Object
Dim shFolder As Object
Dim File, Files
Dim currentPath As String


'get folder
    Set shApp = CreateObject("shell.application")
    folder2search = "C:\Users\ChrisLacs\Desktop\extrac\"
    Set shFolder = shApp.Namespace(folder2search)
'get files
    Set Files = shFolder.Items()
'check if file names match the pattern
    For Each File In Files
        If File.Name Like sPattern Then
       
       
          

Dim wd As New Word.Application
Dim doc As Word.Document
Dim sh As Worksheet

wd.Visible = True

With wd.ActiveDocument
tableTot = .Tables.Count
For tableStart = 1 To tableTot
With .Tables(tableStart)
For iRow = 2 To .Rows.Count ' table rows
ColCnt = 1 'sht column
lr = sh.Cells(Rows.Count, 1).End(xlUp).Row + 1 'sht row
For iCol = 1 To .Columns.Count 'table columns
If .Cell(iRow, iCol).Range.Text = "X" Then
sh.Cells(lr, ColCnt).Value = Application.WorksheetFunction.Clean(.Cell(iRow, iCol).Range.Text)
ColCnt = ColCnt + 1
End If
Next iCol
Next iRow
End With
Next tableStart
End With



           
            Debug.Print File.Name & " - " & (File.Name Like sPattern)
       
        End If
    Next File
End Sub

Sub Search_it()
    Open_Multiple_Word_Files "Form*"
End Sub

It opens only a single word. Other docx with file name Form* does not follow..
 
Upvote 0
Not sure what you mean but you still need to open the doc using the full path before you can access the table data. Dave
 
Upvote 0
A reminder:

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,053
Latest member
Mesh

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