明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 688|回复: 3

Excel提取Word表格内容。小白练手

[复制链接]
发表于 2022-10-19 15:56 | 显示全部楼层 |阅读模式
  1. <div class="blockcode"><blockquote>'wdCell  12  单元格。
  2. 'wdCharacter 1   字符。
  3. 'wdCharacterFormatting   13  字符格式。
  4. 'wdColumn    9   列。
  5. 'wdItem  16  所选项。
  6. 'wdLine  5   一个线段。
  7. 'wdParagraph 4   段落。
  8. 'wdParagraphFormatting   14  段落格式。
  9. 'wdRow   10  行。
  10. 'wdScreen    7   屏幕尺寸。
  11. 'wdSection   8   一节。
  12. 'wdSentence  3   句子。
  13. 'wdStory 6   部分。
  14. 'wdTable 15  一个表格。
  15. 'wdWindow    11  窗口。
  16. 'wdWord  2   字。
  17. 'Expand 扩展
  18. Sub 提取报告工作量稳定版()
  19. Dim a, myrang
  20. '搜索工作量表格关键字
  21. str1 = "建筑物数量"
  22. Set wdapp = CreateObject("word.application")
  23. 'Word窗口显示开
  24. wdapp.Visible = True
  25. '选择文件夹
  26. With Application.FileDialog(msoFileDialogFolderPicker)
  27.         If .Show Then mypath = .SelectedItems(1) Else Exit Sub '打开目标文件夹
  28. End With
  29. '文件地址加“\”
  30.     mypath = mypath & ""
  31. '从第几行开始写入
  32.     r = 2
  33. 'Dir文档名称
  34.     myfile = Dir(mypath)
  35. '检查后缀打开文档
  36. Do While myfile <> ""
  37.   If Right(UCase(myfile), 4) = ".DOC" Or Right(UCase(myfile), 4) = "DOCX" Then '检查扩展名是否为Word文件
  38.   Set wddoc = wdapp.Documents.Open(mypath & myfile) '打开文件
  39. '写入文档名称
  40. Cells(r, 1) = myfile
  41. '激活文档
  42. Set myrange = wdapp.ActiveDocument.Content
  43. '查找str1
  44. With myrange.Find
  45. .Text = str1
  46. .Forward = True
  47. .Execute
  48. If .Found = True Then
  49. myrange.Select
  50. 'wdapp.Selection.Move unit:=10, Count:=1
  51. 'wdapp.Selection.Expand unit:=10
  52. 'MsgBox (wdapp.Selection.Text)
  53. 'MsgBox (wdapp.Selection.tables(1).Columns.Count)
  54. For i = 1 To wdapp.Selection.tables(1).Columns.Count
  55. Cells(r, i + 1).Value = wdapp.Selection.tables(1).Cell(2, i).Range.Text 'Word表格的内容通过该方法获取
  56. Cells(r, i + 1).Value = Left(Cells(r, i + 1).Value, Len(Cells(r, i + 1).Value) - 1) '去除获取内容末尾的黑点(左长度减1来实现)
  57. Next i
  58. Cells(r, i + 1).Value = wdapp.ActiveDocument.Paragraphs(1).Range.Text
  59. Cells(r, i + 1).Value = Left(Cells(r, i + 1).Value, Len(Cells(r, i + 1).Value) - 1)
  60. 'Cells(CellsR, CellsC).Value = wdapp.Selection.tables.Count.Cell(1, 1).Range.Text 'Word表格的内容通过该方法获取
  61. 'Cells(CellsR, CellsC).Value = Left(Cells(CellsR, CellsC).Value, Len(Cells(CellsR, CellsC).Value) - 1) '去除获取内容末尾的黑点
  62. 'Cells(r, 2).Value = wdapp.Selection.Text
  63. 'Cells(r, 3) = Selection.Text
  64. 'For Each i In ActiveDocument.Paragraphs
  65. Else
  66. Cells(r, 2) = "报告内没有此字符"
  67. End If
  68. End With
  69. r = r + 1
  70. wddoc.Close
  71. End If
  72. myfile = Dir()
  73. Loop
  74. wdapp.Quit
  75. MsgBox ("提取出的数据不能进行统计计算" & Chr(10) & "如需统计和计算请另行处理" & Chr(10) & "(可以粘贴到Word再粘贴回Excel)")
  76. End Sub

本小白练手的习题,没看过vb.net,只能用VB宏运行速度慢,如有大神路过请指教优化。
发表于 2022-10-20 14:04 | 显示全部楼层
高手可以加我qq吗3298554767
 楼主| 发表于 2022-10-20 18:27 | 显示全部楼层
我也是小白,只是在论坛学了点皮毛中的皮毛,提高了自己一点点的工作效率!
发表于 2022-11-22 08:09 | 显示全部楼层
谢谢分享,下载尝试下
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-5-4 16:02 , Processed in 0.815236 second(s), 22 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表