|
可试试如下代码。至于同姓名放在一起的问题,可在Excel中自行排序- Sub test()
- '程序在活动Excel文档运行
- Dim WdApp As Object, myDoc As Object
- Dim TF As Boolean, i As Integer, r As Long
- Dim aTable As Object, a(10), n As Integer
- On Error Resume Next
- With Application.FileDialog(msoFileDialogFilePicker)
- .Title = "请选定要处理的Word文档"
- .Filters.Add "Word文档", "*.doc" '暂定扩展名为doc的Word文档
- .AllowMultiSelect = True
- If .Show <> -1 Then Exit Sub
- Set WdApp = GetObject(, "Word.Application")
- If Err <> 0 Then
- TF = True
- Set WdApp = CreateObject("Word.Application")
- End If
- 'WdApp.Visible = True
- r = ActiveSheet.Range("a65536").End(xlUp).Row
- ActiveSheet.Range("A:A").NumberFormatLocal = "@" '设置第一列(编号)的数字格式
- Application.ScreenUpdating = False
- For i = 1 To .SelectedItems.Count
- Set myDoc = WdApp.Documents.Open(.SelectedItems(i))
- For Each aTable In myDoc.Tables
- With aTable '暂提取10个相关数据
- If .Range.Cells.Count = 37 Then '以单元格数为依据对表格进行简单识别
- a(0) = .Range.Previous(Unit:=4).Text
- a(0) = Mid(a(0), 4, Len(a(0)) - 4) '编号
- a(1) = Replace(.Cell(8, 2).Range.Text, Chr(13) & Chr(7), "") '姓名
- a(2) = Replace(.Cell(8, 4).Range.Text, Chr(13) & Chr(7), "")
- a(3) = Replace(.Cell(8, 6).Range.Text, Chr(13) & Chr(7), "")
- a(4) = Replace(.Cell(9, 2).Range.Text, Chr(13) & Chr(7), "") '民族
- a(5) = Replace(.Cell(9, 4).Range.Text, Chr(13) & Chr(7), "")
- a(6) = Replace(.Cell(9, 6).Range.Text, Chr(13) & Chr(7), "")
- a(7) = Replace(.Cell(10, 2).Range.Text, Chr(13) & Chr(7), "") '地址
- a(8) = Replace(.Cell(14, 1).Range.Text, Chr(13) & Chr(7), "") 'CD4
- a(9) = Replace(.Cell(14, 2).Range.Text, Chr(13) & Chr(7), "")
- a(10) = Replace(.Cell(14, 3).Range.Text, Chr(13) & Chr(7), "")
- Range(Cells(r + i, 1), Cells(r + i, 11)).Value = a
- n = n + 1
- End If
- End With
- Next
- myDoc.Close False
- Next i
- End With
- If TF = True Then WdApp.Quit
- Set WdApp = Nothing
- MsgBox "提取完毕!共提取了" & n & "个Word文档。"
- Application.ScreenUpdating = True
- End Sub
复制代码 |
|