Option Explicit
Sub TableCheck()
'https://www.mrexcel.com/forum/excel-questions/1047327-newbie-question-getting-data-word-tables-into-excel.html
'Examine each Word document in sFilePath for tables that have a top row entry of "Needs Work" (case insensitive)
' and counts the number of cells that contain X or x in that column. Reports filename and X count
Dim sFilePath As String
'====================== Update with Word documents file path. Include \ at end ======================
sFilePath = Environ("userprofile") & "\Documents\Evaluations\"
Dim sWorksheet As String
'====================== Update with desired name of worksheet for data ======================
sWorksheet = "Word Doc Table Analysis"
Dim sFileNameExt As String
Dim lNextWriteLine As Long
Dim lWordCount As Long
Dim appWD As Object
Set appWD = CreateObject("Word.Application")
Dim lTblCount As Long
Dim lColCount As Long
Dim tbl As Object
Dim col As Object
Dim celRow As Object
Dim celCol As Object
Dim lCol As Long
Dim lXCount As Long
Dim secAutomation As MsoAutomationSecurity
secAutomation = appWD.Application.AutomationSecurity 'Save Word security setting
appWD.Application.AutomationSecurity = msoAutomationSecurityForceDisable 'Disable macros when opening files
'Delete and recreate worksheet
On Error Resume Next
Application.DisplayAlerts = False
Worksheets(sWorksheet).Delete
Application.DisplayAlerts = True
On Error GoTo 0
Worksheets.Add(after:=Sheets(Sheets.Count)).Name = sWorksheet 'After last
Worksheets(sWorksheet).Range("A1").Resize(1, 2).Value = Array("FileNameExt", "X Count")
'Ensure filepath has a \ at end
If Right(sFilePath, 1) <> "\" Then sFilePath = sFilePath & "\"
appWD.ChangeFileOpenDirectory sFilePath
sFileNameExt = Dir(sFilePath & "*.doc*")
lNextWriteLine = 1
Do While sFileNameExt <> vbNullString
Application.StatusBar = sFileNameExt
lXCount = 0 'Reset document X count
appWD.Documents.Open Filename:=sFilePath & sFileNameExt, ConfirmConversions:=False, _
ReadOnly:=True, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:="", _
Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", _
Format:=0, XMLTransform:="" 'Format:=0=wdOpenFormatAuto
If appWD.activedocument.Tables.Count > 0 Then
For Each tbl In appWD.activedocument.Tables
For Each celRow In tbl.Rows(1).Cells
If InStr(UCase(celRow.Range.Text), "NEEDS WORK") > 0 Then
lCol = celRow.ColumnIndex
For Each celCol In tbl.Columns(lCol).Cells
If InStr(UCase(celCol.Range.Text), "X") > 0 Then
lXCount = lXCount + 1
End If
Next
End If
Next
Next
End If
appWD.activedocument.Close
If lXCount > 0 Then
lNextWriteLine = lNextWriteLine + 1
Worksheets(sWorksheet).Cells(lNextWriteLine, 1).Resize(1, 2).Value = Array(sFileNameExt, lXCount)
End If
sFileNameExt = Dir
Loop
appWD.Application.AutomationSecurity = secAutomation 'Restore Word security setting
'If program quits before following line use Task Manager to close WINWORD.EXE
appWD.Quit
Application.StatusBar = False
End Sub