|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Option Explicit
Sub test() '
Dim wdApp As Object, Items As FileDialogSelectedItems, vItem, strPath$
Dim ar, br, i&, j&, strRngText$
strPath = ThisWorkbook.Path & "\测试文件\"
With Application.FileDialog(1)
With .Filters
.Clear
.Add "Word文档(doc?)", "*.doc?"
End With
.AllowMultiSelect = True
.InitialFileName = strPath
If .Show Then Set Items = .SelectedItems Else Exit Sub
End With
Application.ScreenUpdating = False
br = [{2,3;4,4;6,5;8,6;12,7;14,8;16,9;21,10;23,11;25,12;27,13;29,14;31,15;34,16;36,17;38,18}]
ReDim ar(1 To Items.Count, 1 To 18)
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err <> 0 Then
Set wdApp = CreateObject("Word.Application")
End If
For i = 1 To Items.Count
With wdApp.documents.Open(Items(i))
ar(i, 1) = i
ar(i, 2) = Left(.Paragraphs(3).Range.Text, Len(.Paragraphs(3).Range.Text) - 1)
With .tables(1)
For j = 1 To UBound(br)
strRngText = Left(.Range.Cells(br(j, 1)).Range.Text, Len(.Range.Cells(br(j, 1)).Range.Text) - 2)
ar(i, br(j, 2)) = strRngText
Next j
End With
.Close False
End With
Next i
[A1].CurrentRegion.Offset(1).Clear
With [A2].Resize(UBound(ar), UBound(ar, 2))
.Value = ar
.WrapText = True
.EntireRow.AutoFit
.VerticalAlignment = xlCenter
End With
If Err <> 0 Then wdApp.Quit
Set wdApp = Nothing
Set Items = Nothing
Application.ScreenUpdating = True
Beep
End Sub
|
|