我有一个包含大约300个单页文档的文件夹。每个文档包含大约3个表格和一些文本。例如,在每个文档中都有一个表名为“stackoverflow”的表。如何在多个word文档中找到某个表格并将其提取到一个excel表格中
我有很多像这样的文件,但都不同,除了一个事实,即他们都有一个表中的“计算器”它(如图片中所示)。
我想要做的是从这些表中将所有文档中的所有名称从所有文档中提取到一个Excel表单中。
我试了一下到目前为止,这是一段代码:
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 (*.docx),*.docx", , _
"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
随着这段代码我可以选择我想提取到Excel的表,它完美的作品,除了一个事实,即我有自己输入表格编号,它只适用于一个文档。
我也发现了这一段代码,选择具有一定的字符串里面的表:
Sub Find_Text_in_table()
Selection.Find.ClearFormatting
With Selection.Find
.Text = "donec"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute
If Selection.Information(wdWithInTable) Then
Stop
'now you are in table with text you searched
'be careful with changing Selection Object
'do what you need here
End If
Loop
End Sub
但我不知道如何将这些2
@ShaiRado这是我发现/尝试的 – Gromdroid
表中的文本“表2:StackOverflow”实际上是在表中,还是在表格上方的某种标题? –
它有点棘手,在一些表格中,我认为它是表格的一部分,在其他表格中是其上面的标题。我确实发现,表格中有一个标题,里面有'name'和'adres',否则'table 2:Stackoverflow'在表格中,表格中没有'name'或'adres'。 – Gromdroid