|
楼主 |
发表于 2015-3-6 13:28
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
经过自己学习本坛达人的VBA,现摸索出来了一点点,能抽取一行数据,但一个文档里的另几个同样的表格和同样数据则不能提取。真心希望本坛的老师们能帮助我,谢谢了。
下面是学习老师们的VBA:
Sub GetDocTablletoSheet()
'请在EXCEL VBE中引用MS WORD
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
Dim myArray(11) As String, r As Integer
On Error Resume Next
'定义一个三维数组
strArray = Array("姓名", "居民身份证号", "联系电话", "户籍地址", "实际居住地址")
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 '在所有选取项目中循环
Set wdDoc = wdApp.Documents.Open(Filename:=oSel, Visible:=False)
Set wdTable = wdDoc.Tables(1)
With wdTable '将指定的单元格内容赋值给数组
myArray(0) = Replace(.Cell(3, 2).Range.Text, Chr(13) & Chr(7), "")
myArray(1) = Replace(.Cell(7, 2).Range.Text, Chr(13) & Chr(7), "")
myArray(2) = Replace(.Cell(2, 3).Range.Text, Chr(13) & Chr(7), "")
myArray(3) = Replace(.Cell(8, 2).Range.Text, Chr(13) & Chr(7), "")
myArray(4) = Replace(.Cell(9, 2).Range.Text, Chr(13) & Chr(7), "")
End With
Set wdTable = wdDoc.Tables(2)
With wdTable '将指定的单元格内容赋值给数组
myArray(14) = Replace(.Cell(14, 2).Range.Text, Chr(13) & Chr(7), "")
myArray(18) = Replace(.Cell(18, 3).Range.Text, Chr(13) & Chr(7), "")
myArray(13) = Replace(.Cell(13, 3).Range.Text, Chr(13) & Chr(7), "")
myArray(19) = Replace(.Cell(19, 2).Range.Text, Chr(13) & Chr(7), "")
myArray(10) = Replace(.Cell(20, 2).Range.Text, Chr(13) & Chr(7), "")
End With
wdDoc.Close False
r = r + 1 '以下开始稍作更改
Sheets(1).Range(Cells(r, 1), Cells(r, 5)).Value = myArray '为单元格区域赋值
Next
With Sheets(1)
.Rows(1).Insert
.[A1:H1].Value = strArray
.UsedRange.Columns.AutoFit
End With
End If
End With
wdApp.Quit
Set wdApp = Nothing
Application.ScreenUpdating = True '恢复屏幕更新
End Sub
|
|