|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
不好意思,还是未测试,一开始未看清问题。
1.再次重申,要放到word里面使用
2.自己修改文件夹地址
3.自己在工程里引用Microsoft Scripting Runtime
就是scrrun.dll,如果提示出错就注册一下,开始-运行-"regsvr32 scrrun.dll"
4.代码完成后会生成新Excel文件汇总结果,请保存为xlsx文件,否则有可能不能保存,所以office2003有可能运行此代码的保存部分会有问题- Sub 提取内容_MeThee() '只生成Excel,不自动保存。
- Application.ScreenUpdating = False
- On Error Resume Next
- Dim aCount&, aFolder$, arr, brr, i&, j&
- Dim aTable As Word.Table, aDoc As Word.Document
- '遍历部分
- aFolder = """" & "C:\Users\Administrator\Desktop\我的word文件地址" & """" '设置自己文件夹地址,不要修改引号部分
- With CreateObject("WScript.Shell")
- arr = Split(.Exec(Environ$("comspec") & " /c dir " & Folder & "*.doc /s /a:-d /b | findstr /v " & """" & "~" & """").StdOut.ReadAll, vbCrLf) '黑窗口,一闪即过
- End With
- Dim 姓名 As New Scripting.Dictionary
- Dim 身份证 As New Scripting.Dictionary
- Dim 电话 As New Scripting.Dictionary
- Dim 户籍地 As New Scripting.Dictionary
- Dim 居住地 As New VScripting.Dictionary
- Dim 文件名 As New Scripting.Dictionary
- 姓名(1) = "姓名"
- 身份证(1) = "身份证"
- 电话(1) = "电话"
- 户籍地(1) = "户籍地"
- 居住地(1) = "居住地"
- 文件名(1) = "文件名"
- j = 1
- Lk = LBound(arr): Uk = UBound(arr)
- For k = Lk To Uk '提取内容部分
- With Word.Documents.Open(arr(k))
- aCount = .Tables.Count
- For i = 1 To aCount
- Set aTable = .Tables(i)
- brr = Split(aTable.Range.Text, Chr(7))
- If UBound(brr) = 108 Then
- j = j + 1
- 姓名.Add j, Left(brr(13), Len(brr(13)) - 1)
- 身份证.Add j, Left(brr(39), Len(brr(39)) - 1)
- 电话.Add j, Left(brr(8), Len(brr(8)) - 1)
- 户籍地.Add j, Left(brr(44), Len(brr(44)) - 1)
- 居住地.Add j, Left(brr(49), Len(brr(49)) - 1)
- 文件名.Add j, arr(k)
- j = j + 1
- 姓名.Add j, Left(brr(66), Len(brr(66)) - 1)
- 身份证.Add j, Left(brr(92), Len(brr(92)) - 1)
- 电话.Add j, Left(brr(61), Len(brr(61)) - 1)
- 户籍地.Add j, Left(brr(97), Len(brr(97)) - 1)
- 居住地.Add j, Left(brr(102), Len(brr(102)) - 1)
- 文件名.Add j, arr(k)
- End If
- Next
- End With
- Next
- Set aExcel = CreateObject("Excel.Application")
- aExcel.Visible = True
- Set aBook = aExcel.Workbooks.Add
- Set aSheet = aBook.Worksheets("Sheet1")
- aCount = 姓名.Count
- '保存部分
- aSheet.Range("A1").Resize(aCount, 1) = aExcel.Transpose(姓名.Items)
- aSheet.Range("B1").Resize(aCount, 1) = aExcel.Transpose(身份证.Items)
- aSheet.Range("C1").Resize(aCount, 1) = aExcel.Transpose(电话.Items)
- aSheet.Range("D1").Resize(aCount, 1) = aExcel.Transpose(户籍地.Items)
- aSheet.Range("E1").Resize(aCount, 1) = aExcel.Transpose(居住地.Items)
- aSheet.Range("F1").Resize(aCount, 1) = aExcel.Transpose(文件名.Items)
- Application.ScreenUpdating = True
- End Sub
复制代码 |
|