|
又花了近1天时间,刚刚实现了“荣获情况”行出现在任何一行,都可以实现相同结果的效果。终于自认为比较圆满了。主要是红色部分代码。
Sub GetDocTablletoSheet()
'请在EXCEL VBE中引用MS WORD http://xysj1980.blog.163.com/blo ... 398201221843054403/
Dim wdApp As Word.Application, wdDoc As Word.Document, wdTable As Word.Table
Dim strArray() As Variant, xlSheet As Worksheet, myDialog As FileDialog, oSel As Variant, otb As Table
Dim myArray() As String, r As Integer, i As Integer, m As Integer, rc As Integer
For Each Process In GetObject("winmgmts:").ExecQuery("select * from Win32_Process where name='WINWORD.EXE'")
Process.Terminate (0)
Next
On Error Resume Next
r = ActiveSheet.[b65536].End(xlUp).Row
'定义一个一维数组,给EXCEL数据表表头赋值
If r > 2 Then
Range("b2:m" & r).ClearContents
End If
r = ActiveSheet.[b65536].End(xlUp).Row
Set wdApp = New Word.Application '取得一个New Word对象
Set myDialog = Application.FileDialog(msoFileDialogFilePicker)
With myDialog
.Filters.Clear '清除所有文件筛选器中的项目
.Filters.Add "所有 WORD 文件", "*.doc", 1 '增加筛选器的项目为所有WORD文件
.AllowMultiSelect = True '允许多项选择
If .Show = -1 Then '确定
Application.ScreenUpdating = False
For Each oSel In .SelectedItems '在所有选取word文档中循环
m = 8 '此处数字2即提取word表格中几个数据,亦即提取数据后工作表中列数
Set wdDoc = wdApp.Documents.Open(Filename:=oSel, Visible:=False)
For i = 1 To wdDoc.Tables.Count '在一个word文档的所有表格中循环
Set wdTable = wdDoc.Tables(i)
With wdTable
For ii = 1 To .Range.Rows.Count
If Replace(.cell(ii, 1).Range.Text, Chr(13) & Chr(7), "") = "荣获情况" Then
rc = ii - 1
Exit For
End If
Next
For rc1 = rc To 7 Step -1
If Replace(.cell(rc1, 1).Range.Text, Chr(13) & Chr(7), "") = "" Then
rc = rc - 1
End If
Next
End With
rm = rc - 6
ReDim myArray(1 To rm, 1 To 12) '此处需要加入统计某一内容的行数
With wdTable '将word文档中指定的单元格内容赋值给数组
For a1 = 1 To rm
myArray(a1, 1) = Replace(.cell(1, 2).Range.Text, Chr(13) & Chr(7), "")
myArray(a1, 2) = Replace(.cell(1, 4).Range.Text, Chr(13) & Chr(7), "")
myArray(a1, 3) = Replace(.cell(1, 6).Range.Text, Chr(13) & Chr(7), "")
myArray(a1, 4) = Replace(.cell(2, 2).Range.Text, Chr(13) & Chr(7), "")
myArray(a1, 5) = Replace(.cell(2, 4).Range.Text, Chr(13) & Chr(7), "")
myArray(a1, 6) = Replace(.cell(2, 6).Range.Text, Chr(13) & Chr(7), "")
myArray(a1, 7) = Replace(.cell(4, 1).Range.Text, Chr(13) & Chr(7), "")
Next
For r1 = 1 To rm
For r2 = 8 To 12 - 1
myArray(r1, r2) = Replace(.cell(r1 + 6, r2 - 7).Range.Text, Chr(13) & Chr(7), "")
Next
Next
End With
With Sheets(1)
r = .[b65536].End(xlUp).Row + 1
.Range("B" & r).Resize(UBound(myArray), UBound(myArray, 2)) = myArray '为单元格区域赋值
For rn = r To r + rm
.Hyperlinks.Add Anchor:=Cells(rn, 13), Address:=wdDoc.Name, SubAddress:="", TextToDisplay:="" 'wdDoc.Name '文件名超链接
Next
End With
Next '完成一个文件的赋值
wdDoc.Close False
Next
End If
End With
wdApp.Quit
Set wdApp = Nothing
Application.ScreenUpdating = True '恢复屏幕更新
End Sub
|
|