Sub FileDir()
Dim p$, f$, k&, arr(), i%, n%, wdApp, y&
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show Then p = .SelectedItems(1) Else Exit Sub
End With
If Right(p, 1) <> "\" Then p = p & "\"
f = Dir(p & "*.docx")
ReDim arr(1 To 4, 1 To 1000)
Set wdApp = CreateObject("word.application")
wdApp.Visible = False
y = 0
Do While f <> ""
With wdApp.Documents.Open(p & f)
For Each tb In .tables
y = y + 1
If y > UBound(arr, 2) Then ReDim Preserve arr(1 To 4, 1 To UBound(arr, 2) + 1000)
arr(1, y) = Replace(tb.Cell(1, 2).Range.Text, Chr(7), "")
arr(2, y) = Replace(tb.Cell(1, 4).Range.Text, Chr(7), "")
arr(3, y) = Replace(tb.Cell(2, 2).Range.Text, Chr(7), "")
arr(4, y) = Replace(tb.Cell(3, 2).Range.Text, Chr(7), "")
Next
.Close
End With
f = Dir
Loop
Sheet1.Range("A" & 2).Resize(y, 4) = WorksheetFunction.Transpose(arr)
wdApp.Quit
End Sub
你源代码中,打开文档的时候需要使用完整路径加文件名 |