'WORD 加载项 代码模板
Dim cmdBar As CommandBar, cmdBtn As CommandBarControl
Const cmdBtnCap As String = "批量提取操作步骤" Sub AutoExec() Call DelCmdBtn
Call AddCmdBtn End Sub
Sub AutoExit()
Call DelCmdBtn
End Sub Sub AddCmdBtn() Set cmdBar = Application.CommandBars("Tools") Set cmdBtn = cmdBar.Controls.Add(msoControlButton)
With cmdBtn
.Caption = cmdBtnCap
.Style = msoButtonCaption
.OnAction = "GetContents"
End With Set cmdBtn = Nothing
Set cmdBar = Nothing End Sub
Sub DelCmdBtn()
Set cmdBar = Application.CommandBars("Tools")
For Each cmdBtn In cmdBar.Controls
If cmdBtn.Caption = cmdBtnCap Then cmdBtn.Delete
Next Set cmdBtn = Nothing
Set cmdBar = Nothing
End Sub Public Sub GetContents() Application.ScreenUpdating = False Dim xlApp As Object
Dim Wb As Object
Dim Sht As Object
Dim Rng As Object
Dim OpenDoc As Document Dim ExcelPath As String
Const ExcelFile As String = "未完成.xls" Dim FolderPath As String
Dim FilePath As String
Dim FileName As String ExcelPath = ThisDocument.Path & "\" & ExcelFile With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = ThisDocument.Path
.AllowMultiSelect = False
.Title = "请选取Word所在文件夹"
If .Show = -1 Then
FolderPath = .SelectedItems(1)
Else
MsgBox "您没有选中任何文件夹,本次汇总中断!"
Exit Sub
End If
End With s = Split(FolderPath, "\")
c = UBound(s)
ShtName = s(c) If Right(FolderPath, 1) <> "\" Then FolderPath = FolderPath & "\" On Error Resume Next
Set xlApp = GetObject(, "Excel.Application")
If xlApp Is Nothing Then
Set xlApp = CreateObject("Excel.Application")
End If
On Error GoTo 0 Set Wb = xlApp.workbooks.Open(ExcelPath)
Set Sht = Wb.worksheets.Add()
Sht.Name = ShtName
Sht.Cells.clearcontents
Sht.Range("A1:D1").Value = Array("操作编号", "操作任务", "操作序号", "操作步骤") FileName = Dir(FolderPath & "*.doc*")
Do While FileName <> ""
FilePath = FolderPath & FileName
If FileName <> ThisDocument.Name Then
Set OpenDoc = Application.Documents.Open(FilePath)
'If OpenDoc.Tables.Count > 0 Then
Arr = GetArray(OpenDoc) Debug.Print Arr(3, 1) Sht.Cells(Sht.Rows.Count, 2).End(3).offset(1).Resize(UBound(Arr, 2), UBound(Arr)).Value = _
xlApp.worksheetfunction.transpose(Arr) 'End If
OpenDoc.Close False
End If
FileName = Dir
Loop Wb.Close True
xlApp.Quit 'MsgBox "本次提取完成!" 'Application.ScreenUpdating = True
End Sub Function GetArray(ByVal Doc As Document) As Variant
Dim tb As Table
Dim tbCount As Long
Dim RecordStart As Boolean
Dim RecordEnd As Boolean
Dim Arr() As String
Dim Mission As String Doc.Activate
If Selection.Type = wdSelectionIP Then
ActiveDocument.Content.ListFormat.ConvertNumbersToText
ActiveDocument.Content.Find.Execute FindText:="^t", replacewith:=" ", Replace:=wdReplaceAll
Else
Selection.Range.ListFormat.ConvertNumbersToText
Selection.Find.Execute FindText:="^t", replacewith:=" ", Replace:=wdReplaceAll
End If ReDim Arr(1 To 3, 1 To 1)
Index = 0 RecordStart = False
RecordEnd = False tbCount = Doc.Tables.Count
If tbCount > 0 Then
n = 0
For Each tb In Doc.Tables With tb
For i = 1 To .Rows.Count
'Debug.Print tb.Rows(3).Cells(1).Range.Text
If tb.Rows(3).Cells(1).Range.Text Like "*操作任务*" And Mission = "" Then
Mission = tb.Rows(3).Cells(1).Range.Text
Mission = RegGet(Mission, "操作任务[::](\S+?)\s+?")
'Debug.Print Mission
End If If .Rows(i).Cells.Count = 5 Then
If .Rows(i).Cells(1).Range.Text Like "*#*" And _
.Rows(i).Cells(3).Range.Text Like "*得令*" Then
'Debug.Print .Rows(i).Cells(3).Range.Text
RecordStart = True
End If
If .Rows(i).Cells(1).Range.Text Like "*#*" Or .Rows(i).Cells(1).Range.Text = "" And RecordStart = True And RecordEnd = False Then
Index = Index + 1
ReDim Preserve Arr(1 To 3, 1 To Index)
Arr(1, Index) = Mission
Debug.Print Mission
Arr(2, Index) = Replace(Replace(.Rows(i).Cells(1).Range.Text, Chr(7), ""), vbCr, "")
Arr(3, Index) = Replace(Replace(.Rows(i).Cells(3).Range.Text, Chr(7), ""), vbCr, "")
End If If .Rows(i).Cells(1).Range.Text Like "*#*" And _
.Rows(i).Cells(3).Range.Text Like "*汇报*" Then
RecordStart = False
RecordEnd = True
GoTo ExitFunction
End If
End If
Next i
End With
Next tb
End If ExitFunction:
GetArray = Arr End Function
Public Function RegGet(ByVal OrgText As String, ByVal Pattern As String) As String
'传递参数 :原字符串, 匹配模式
Dim Regex As Object
Dim Mh As Object
Set Regex = CreateObject("VBScript.RegExp")
With Regex
.Global = True
.Pattern = Pattern
End With
If Regex.test(OrgText) Then
Set Mh = Regex.Execute(OrgText)
RegGet = Mh.Item(0).submatches(0)
Else
RegGet = ""
End If
Set Regex = Nothing
End Function
Sub 自动编号转文本()
If Selection.Type = wdSelectionIP Then
ActiveDocument.Content.ListFormat.ConvertNumbersToText
ActiveDocument.Content.Find.Execute FindText:="^t", replacewith:=" ", Replace:=wdReplaceAll
Else
Selection.Range.ListFormat.ConvertNumbersToText
Selection.Find.Execute FindText:="^t", replacewith:=" ", Replace:=wdReplaceAll
End If
End Sub

  

20170907wdVBA_GetCellsContentToExcel的更多相关文章

随机推荐

  1. 查看Windows系统信息

    在终端输入"msinfo32". ms>Microsoft info>information

  2. ODAC(V9.5.15) 学习笔记(四)TMemDataSet (3)

    3.其他 名称 类型 说明 GetBlob TBlob 按照字段名获取当前数据集中某个Blob类型的字段值,并以TBlob对象形式返回 Prepared Boolean 检查Query的SQL是否已准 ...

  3. Python3基础 list [] 创建空列表

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

  4. MongoDB ReplicaSet 集群搭建

    说明 本文创建的集群的名字为test,在同一台机器上创建了三个mongo实例,端口不同即可. 安装mongodb的教程,之前总结过,请参考:CentOS安装MongoDB笔记 创建实例 # 本机默认原 ...

  5. 【POJ1961】period

    [POJ1961]period 题目描述 如果一个字符串S是由一个字符串T重复K次构成的,则称T是S的循环元.使K出现最大的字符串T称为S的最小循环元,此时的K称为最大循环次数. 现在给定一个长度为N ...

  6. UVA 818 Cutting Chains(状压 + 暴搜)题解

    题意:有1~n个小环,他们中的有些互相扣在一起,问你至少切开几个能把这写小环串成一条链 思路:还是太菜了,题目给的n<=15,显然可以暴力解决. 用二进制表示每个环切还是不切,然后搜索所有情况. ...

  7. asp.net core mvc 中在C# 代码中写 js 或html 文本

    https://blog.csdn.net/orichisonic/article/details/62046621 使用<text>这个伪元素来强制Razor从编译模式返回到内容模式: ...

  8. P3159 [CQOI2012]交换棋子

    思路 相当神奇的费用流拆点模型 最开始我想到把交换黑色棋子看成一个流流动的过程,流从一个节点流向另一个节点就是交换两个节点,然后把一个位置拆成两个点限制流量,然后就有了这样的建图方法 S向所有初始是黑 ...

  9. 论文笔记:Semantic Segmentation using Adversarial Networks

    Semantic Segmentation using Adversarial Networks 2018-04-27 09:36:48 Abstract: 对于产生式图像建模来说,对抗训练已经取得了 ...

  10. JZ2440之GPIO篇

    买来开发板已经有一段时间了,刚接触时兴奋至极,后来跟着视频看下去发现似乎自己并没有学到太多东西,于是发现自己可能欠缺的太多以致从课程中无法提取出重要的东西来,所以并没有得到太多的营养成分.因此我个人认 ...