使用VBA提取文件夹内所有word文档中的表格数据

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

使用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 myfile In myfiles
m = m + 1
Set wdapp = CreateObject("word.application")
wdapp.Documents.Open myfile.Path
'wdapp.Visible = False
wdapp.Visible = True
Set mydoc = wdapp.Documents.Item()
For Each mTable In mydoc.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 = TextBox1.Text + vbCrLf + "已完成第" + Str(m) + "项:" + myfile.Path n = 1
Next
TextBox1.SetFocus
TextBox1.Text = TextBox1.Text + vbCrLf + "全部完成!共计" + Str(m) + "项"
MsgBox ("全部完成!共计" + Str(m) + "项")
End Sub。

相关文档
最新文档