'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. Python 处理 CSV/EXCEL 表格文件

    只想说,数据挖掘工作,80%时间都花在处理数据上了,这句话真不假! 最近和小伙伴组了个队参加数据分析比赛,记录下我处理 csv 文件的一些步骤吧: 修改csv文件 可以用csv模块1,官方文档2 im ...

  2. 螺旋折线(可能是最简单的找规律)【蓝桥杯2018 C/C++ B组】

    标题:螺旋折线 如图p1.png所示的螺旋折线经过平面上所有整点恰好一次.   对于整点(X, Y),我们定义它到原点的距离dis(X, Y)是从原点到(X, Y)的螺旋折线段的长度. 例如dis(0 ...

  3. gimp的使用笔记

    gimp是德国的开源软件! 跟其他软件一样, 包括file, edit, view, 还有select, color , filter, 和 window. 窗口window就包括所有的dockabl ...

  4. 2870: 最长道路tree

    链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2870 思路 先把树转化为二叉树 再链分治 %%yyb 代码 #include <ios ...

  5. 在Linux安装和使用LinuxBrew

    简介 LinuxBrew是流行的Mac OS X的一个Linux叉自制包管理器. LinuxBrew是包管理软件,它能从源(在Debian / Ubuntu的如"易/ DEB",并 ...

  6. size_t和unsigned int区别

    size_t和unsigned int有所不同,size_t的取值range是目标平台下最大可能的数组尺寸,一些平台下size_t的范围小于int的正数范围,又或者大于unsigned int.最典型 ...

  7. pyqt笔记1模块 信号和插槽

    资料 PyQt5图形界面编程 PyQt5指南 模块 PyQt5本身拥有超过620个类和6000函数及方法. QtCore模块涵盖了包的核心的非GUI功能,此模块被用于处理程序中涉及到的 time.文件 ...

  8. js旋转V字俄罗斯方块

    实现效果如图,也就是一个图像的旋转.注意,旋转后的文字是相对应的,而且文字还是立起的.第一次点击时显示,第二次点击时开始旋转.下面是我做这个效果的记录,方法这么差,我也就不说什么了. 先上HTML/C ...

  9. 【Selenium2】【Jenkins】

    1. 下载Tomcat ,Windows7 环境,http://tomcat.apache.org/  我下载的是版本8 2. 下载Jenkins,Windows7 环境,http://jenkins ...

  10. 三: 爬虫之selenium模块

    一 selenium模块 什么是selenium?selenium是Python的一个第三方库,对外提供的接口可以操作浏览器,然后让浏览器完成自动化的操作. selenium最初是一个自动化测试工具, ...