1. 'WORD 加载项 代码模板
  2. Dim cmdBar As CommandBar, cmdBtn As CommandBarControl
  3. Const cmdBtnCap As String = "批量提取操作步骤"
  4.  
  5. Sub AutoExec()
  6.  
  7. Call DelCmdBtn
  8. Call AddCmdBtn
  9.  
  10. End Sub
  11. Sub AutoExit()
  12. Call DelCmdBtn
  13. End Sub
  14.  
  15. Sub AddCmdBtn()
  16.  
  17. Set cmdBar = Application.CommandBars("Tools")
  18.  
  19. Set cmdBtn = cmdBar.Controls.Add(msoControlButton)
  20. With cmdBtn
  21. .Caption = cmdBtnCap
  22. .Style = msoButtonCaption
  23. .OnAction = "GetContents"
  24. End With
  25.  
  26. Set cmdBtn = Nothing
  27. Set cmdBar = Nothing
  28.  
  29. End Sub
  30. Sub DelCmdBtn()
  31. Set cmdBar = Application.CommandBars("Tools")
  32. For Each cmdBtn In cmdBar.Controls
  33. If cmdBtn.Caption = cmdBtnCap Then cmdBtn.Delete
  34. Next
  35.  
  36. Set cmdBtn = Nothing
  37. Set cmdBar = Nothing
  38. End Sub
  39.  
  40. Public Sub GetContents()
  41.  
  42. Application.ScreenUpdating = False
  43.  
  44. Dim xlApp As Object
  45. Dim Wb As Object
  46. Dim Sht As Object
  47. Dim Rng As Object
  48. Dim OpenDoc As Document
  49.  
  50. Dim ExcelPath As String
  51. Const ExcelFile As String = "未完成.xls"
  52.  
  53. Dim FolderPath As String
  54. Dim FilePath As String
  55. Dim FileName As String
  56.  
  57. ExcelPath = ThisDocument.Path & "\" & ExcelFile
  58.  
  59. With Application.FileDialog(msoFileDialogFolderPicker)
  60. .InitialFileName = ThisDocument.Path
  61. .AllowMultiSelect = False
  62. .Title = "请选取Word所在文件夹"
  63. If .Show = -1 Then
  64. FolderPath = .SelectedItems(1)
  65. Else
  66. MsgBox "您没有选中任何文件夹,本次汇总中断!"
  67. Exit Sub
  68. End If
  69. End With
  70.  
  71. s = Split(FolderPath, "\")
  72. c = UBound(s)
  73. ShtName = s(c)
  74.  
  75. If Right(FolderPath, 1) <> "\" Then FolderPath = FolderPath & "\"
  76.  
  77. On Error Resume Next
  78. Set xlApp = GetObject(, "Excel.Application")
  79. If xlApp Is Nothing Then
  80. Set xlApp = CreateObject("Excel.Application")
  81. End If
  82. On Error GoTo 0
  83.  
  84. Set Wb = xlApp.workbooks.Open(ExcelPath)
  85. Set Sht = Wb.worksheets.Add()
  86. Sht.Name = ShtName
  87. Sht.Cells.clearcontents
  88. Sht.Range("A1:D1").Value = Array("操作编号", "操作任务", "操作序号", "操作步骤")
  89.  
  90. FileName = Dir(FolderPath & "*.doc*")
  91. Do While FileName <> ""
  92. FilePath = FolderPath & FileName
  93. If FileName <> ThisDocument.Name Then
  94. Set OpenDoc = Application.Documents.Open(FilePath)
  95. 'If OpenDoc.Tables.Count > 0 Then
  96. Arr = GetArray(OpenDoc)
  97.  
  98. Debug.Print Arr(3, 1)
  99.  
  100. Sht.Cells(Sht.Rows.Count, 2).End(3).offset(1).Resize(UBound(Arr, 2), UBound(Arr)).Value = _
  101. xlApp.worksheetfunction.transpose(Arr)
  102.  
  103. 'End If
  104. OpenDoc.Close False
  105. End If
  106. FileName = Dir
  107. Loop
  108.  
  109. Wb.Close True
  110. xlApp.Quit
  111.  
  112. 'MsgBox "本次提取完成!"
  113.  
  114. 'Application.ScreenUpdating = True
  115. End Sub
  116.  
  117. Function GetArray(ByVal Doc As Document) As Variant
  118. Dim tb As Table
  119. Dim tbCount As Long
  120. Dim RecordStart As Boolean
  121. Dim RecordEnd As Boolean
  122. Dim Arr() As String
  123. Dim Mission As String
  124.  
  125. Doc.Activate
  126. If Selection.Type = wdSelectionIP Then
  127. ActiveDocument.Content.ListFormat.ConvertNumbersToText
  128. ActiveDocument.Content.Find.Execute FindText:="^t", replacewith:=" ", Replace:=wdReplaceAll
  129. Else
  130. Selection.Range.ListFormat.ConvertNumbersToText
  131. Selection.Find.Execute FindText:="^t", replacewith:=" ", Replace:=wdReplaceAll
  132. End If
  133.  
  134. ReDim Arr(1 To 3, 1 To 1)
  135. Index = 0
  136.  
  137. RecordStart = False
  138. RecordEnd = False
  139.  
  140. tbCount = Doc.Tables.Count
  141. If tbCount > 0 Then
  142. n = 0
  143. For Each tb In Doc.Tables
  144.  
  145. With tb
  146. For i = 1 To .Rows.Count
  147. 'Debug.Print tb.Rows(3).Cells(1).Range.Text
  148. If tb.Rows(3).Cells(1).Range.Text Like "*操作任务*" And Mission = "" Then
  149. Mission = tb.Rows(3).Cells(1).Range.Text
  150. Mission = RegGet(Mission, "操作任务[::](\S+?)\s+?")
  151. 'Debug.Print Mission
  152. End If
  153.  
  154. If .Rows(i).Cells.Count = 5 Then
  155. If .Rows(i).Cells(1).Range.Text Like "*#*" And _
  156. .Rows(i).Cells(3).Range.Text Like "*得令*" Then
  157. 'Debug.Print .Rows(i).Cells(3).Range.Text
  158. RecordStart = True
  159. End If
  160. If .Rows(i).Cells(1).Range.Text Like "*#*" Or .Rows(i).Cells(1).Range.Text = "" And RecordStart = True And RecordEnd = False Then
  161. Index = Index + 1
  162. ReDim Preserve Arr(1 To 3, 1 To Index)
  163. Arr(1, Index) = Mission
  164. Debug.Print Mission
  165. Arr(2, Index) = Replace(Replace(.Rows(i).Cells(1).Range.Text, Chr(7), ""), vbCr, "")
  166. Arr(3, Index) = Replace(Replace(.Rows(i).Cells(3).Range.Text, Chr(7), ""), vbCr, "")
  167. End If
  168.  
  169. If .Rows(i).Cells(1).Range.Text Like "*#*" And _
  170. .Rows(i).Cells(3).Range.Text Like "*汇报*" Then
  171. RecordStart = False
  172. RecordEnd = True
  173. GoTo ExitFunction
  174. End If
  175. End If
  176. Next i
  177. End With
  178. Next tb
  179. End If
  180.  
  181. ExitFunction:
  182. GetArray = Arr
  183.  
  184. End Function
  185. Public Function RegGet(ByVal OrgText As String, ByVal Pattern As String) As String
  186. '传递参数 :原字符串, 匹配模式
  187. Dim Regex As Object
  188. Dim Mh As Object
  189. Set Regex = CreateObject("VBScript.RegExp")
  190. With Regex
  191. .Global = True
  192. .Pattern = Pattern
  193. End With
  194. If Regex.test(OrgText) Then
  195. Set Mh = Regex.Execute(OrgText)
  196. RegGet = Mh.Item(0).submatches(0)
  197. Else
  198. RegGet = ""
  199. End If
  200. Set Regex = Nothing
  201. End Function
  202. Sub 自动编号转文本()
  203. If Selection.Type = wdSelectionIP Then
  204. ActiveDocument.Content.ListFormat.ConvertNumbersToText
  205. ActiveDocument.Content.Find.Execute FindText:="^t", replacewith:=" ", Replace:=wdReplaceAll
  206. Else
  207. Selection.Range.ListFormat.ConvertNumbersToText
  208. Selection.Find.Execute FindText:="^t", replacewith:=" ", Replace:=wdReplaceAll
  209. End If
  210. End Sub

  

20170907wdVBA_GetCellsContentToExcel的更多相关文章

随机推荐

  1. AppStore 添加回复

    itunes connect 评论位置 1, 2, 添加用户权限:除了管理和客户支持可以回复.开发人员等只有只读权限

  2. EGIT

    https://jingyan.baidu.com/article/64d05a0262f013de55f73bcc.html

  3. 解决Tax discount configure 报出异常

    If your tax calculation is based on a problematic configuration, the following warnings appear: Warn ...

  4. Spring 学习——Resources接口

    Resources 针对资源文件的统一接口 Resources UrlResource:URL对应的资源,只需要一个url即可构建 ClassPathResource:获取类路径下的资源文件 File ...

  5. Python3基础 list for+continue 输出1-50之间的偶数

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  6. WEB安全学习二、注入工具 sqlmap的使用

    使用的是Kali Linux 系统,系统中默认的sqlmap 是安装好了的,电脑上没有安装sqlmap,自己百度  ,需要python的环境 使用 命令   sqlmap -h 可以查看   sqlm ...

  7. 剥开比原看代码11:比原是如何通过接口/create-account创建帐户的

    作者:freewind 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchai ...

  8. MongoDB集群配置笔记二(实战)

    单台mongodb配置文件: dbpath=/opt/mongodb/data logpath=/opt/mongodb/logs/mongodb.log logappend=true fork=tr ...

  9. 2、Python程序控制结构(0530)

    条件测试: 1.if 条件测试表达式 python的比较操作 1.所有的python对象都支持比较操作 可用于测试相等性.相对大小等: 如果是符合对象,python会检查其所有部分,包括自动遍历各级嵌 ...

  10. mysql联合主键自增、主键最大长度小记

    前言 一. 联合主键自增问题 今天上午闲来无事翻看了下数据库分类表的设计,看到这样一幕: 当时我好奇的是怎么cateId自增会存在重复值的问题,然后翻看了下主键是由siteId和cateId组成.所以 ...