使用VBA提取word文档中的表格数据
新建一个excel文档,在VBA编辑界面操作
添加用户界面:
代码:
Private Sub CommandButton1_Click()
Dim s As String
s = TextBox2.Text
Dim fs, myfolder, myfile, myfiles, wdapp, mydoc
Dim mTable As Table, mCell As Cell
Set fs = CreateObject("Scripting.FileSystemObject")
Set myfolder = fs.GetFolder(s)
Set myfiles = myfolder.Files
Dim m, n As Integer
m = 0
n = 1
For Each myfileInmyfiles
m = m + 1
Set wdapp = CreateObject("word.application")
wdapp.Documents.Openmyfile.Path
'wdapp.Visible = False
wdapp.Visible = True
Set mydoc = wdapp.Documents.Item(https://www.wendangku.net/doc/8b14131675.html,)
For Each mTableInmydoc.Tables
For Each mCell In mTable.Range.Cells
ThisWorkbook.ActiveSheet.Cells(m, n) = Left(mCell.Range.Text, Len(mCell.Range.Text) - 1) n = n + 1
Next mCell
Next mTable
Set mydoc = Nothing
wdapp.Quit
TextBox1.Text = T extBox1.Text + vbCrLf + "已完成第" + Str(m) + "项:" +myfile.Path n = 1
Next
TextBox1.SetFocus
TextBox1.Text = T extBox1.Text + vbCrLf + "全部完成!共计" + Str(m) + "项"
MsgBox ("全部完成!共计" + Str(m) + "项")
End Sub