20170907wdVBA_GetCellsContentToExcel
2024-05-13 09:49:45
'WORD 加载项 代码模板
Dim cmdBar As CommandBar, cmdBtn As CommandBarControl
Const cmdBtnCap As String = "批量提取操作步骤"Sub AutoExec()Call DelCmdBtnCall AddCmdBtnEnd Sub
Sub AutoExit()Call DelCmdBtn
End SubSub AddCmdBtn()Set cmdBar = Application.CommandBars("Tools")Set cmdBtn = cmdBar.Controls.Add(msoControlButton)With cmdBtn.Caption = cmdBtnCap.Style = msoButtonCaption.OnAction = "GetContents"End WithSet cmdBtn = NothingSet cmdBar = NothingEnd Sub
Sub DelCmdBtn()Set cmdBar = Application.CommandBars("Tools")For Each cmdBtn In cmdBar.ControlsIf cmdBtn.Caption = cmdBtnCap Then cmdBtn.DeleteNextSet cmdBtn = NothingSet cmdBar = Nothing
End SubPublic Sub GetContents()Application.ScreenUpdating = FalseDim xlApp As ObjectDim Wb As ObjectDim Sht As ObjectDim Rng As ObjectDim OpenDoc As DocumentDim ExcelPath As StringConst ExcelFile As String = "未完成.xls"Dim FolderPath As StringDim FilePath As StringDim FileName As StringExcelPath = ThisDocument.Path & "\" & ExcelFileWith Application.FileDialog(msoFileDialogFolderPicker).InitialFileName = ThisDocument.Path.AllowMultiSelect = False.Title = "请选取Word所在文件夹"If .Show = -1 ThenFolderPath = .SelectedItems(1)ElseMsgBox "您没有选中任何文件夹,本次汇总中断!"Exit SubEnd IfEnd Withs = Split(FolderPath, "\")c = UBound(s)ShtName = s(c)If Right(FolderPath, 1) <> "\" Then FolderPath = FolderPath & "\"On Error Resume NextSet xlApp = GetObject(, "Excel.Application")If xlApp Is Nothing ThenSet xlApp = CreateObject("Excel.Application")End IfOn Error GoTo 0Set Wb = xlApp.workbooks.Open(ExcelPath)Set Sht = Wb.worksheets.Add()Sht.Name = ShtNameSht.Cells.clearcontentsSht.Range("A1:D1").Value = Array("操作编号", "操作任务", "操作序号", "操作步骤")FileName = Dir(FolderPath & "*.doc*")Do While FileName <> ""FilePath = FolderPath & FileNameIf FileName <> ThisDocument.Name ThenSet OpenDoc = Application.Documents.Open(FilePath)'If OpenDoc.Tables.Count > 0 ThenArr = 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 IfOpenDoc.Close FalseEnd IfFileName = DirLoopWb.Close TruexlApp.Quit'MsgBox "本次提取完成!"'Application.ScreenUpdating = True
End SubFunction GetArray(ByVal Doc As Document) As VariantDim tb As TableDim tbCount As LongDim RecordStart As BooleanDim RecordEnd As BooleanDim Arr() As StringDim Mission As StringDoc.ActivateIf Selection.Type = wdSelectionIP ThenActiveDocument.Content.ListFormat.ConvertNumbersToTextActiveDocument.Content.Find.Execute FindText:="^t", replacewith:=" ", Replace:=wdReplaceAllElseSelection.Range.ListFormat.ConvertNumbersToTextSelection.Find.Execute FindText:="^t", replacewith:=" ", Replace:=wdReplaceAllEnd IfReDim Arr(1 To 3, 1 To 1)Index = 0RecordStart = FalseRecordEnd = FalsetbCount = Doc.Tables.CountIf tbCount > 0 Thenn = 0For Each tb In Doc.TablesWith tbFor i = 1 To .Rows.Count'Debug.Print tb.Rows(3).Cells(1).Range.TextIf tb.Rows(3).Cells(1).Range.Text Like "*操作任务*" And Mission = "" ThenMission = tb.Rows(3).Cells(1).Range.TextMission = RegGet(Mission, "操作任务[::](\S+?)\s+?")'Debug.Print MissionEnd IfIf .Rows(i).Cells.Count = 5 ThenIf .Rows(i).Cells(1).Range.Text Like "*#*" And _.Rows(i).Cells(3).Range.Text Like "*得令*" Then'Debug.Print .Rows(i).Cells(3).Range.TextRecordStart = TrueEnd IfIf .Rows(i).Cells(1).Range.Text Like "*#*" Or .Rows(i).Cells(1).Range.Text = "" And RecordStart = True And RecordEnd = False ThenIndex = Index + 1ReDim Preserve Arr(1 To 3, 1 To Index)Arr(1, Index) = MissionDebug.Print MissionArr(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 IfIf .Rows(i).Cells(1).Range.Text Like "*#*" And _.Rows(i).Cells(3).Range.Text Like "*汇报*" ThenRecordStart = FalseRecordEnd = TrueGoTo ExitFunctionEnd IfEnd IfNext iEnd WithNext tbEnd IfExitFunction:GetArray = ArrEnd Function
Public Function RegGet(ByVal OrgText As String, ByVal Pattern As String) As String
'传递参数 :原字符串, 匹配模式Dim Regex As ObjectDim Mh As ObjectSet Regex = CreateObject("VBScript.RegExp")With Regex.Global = True.Pattern = PatternEnd WithIf Regex.test(OrgText) ThenSet Mh = Regex.Execute(OrgText)RegGet = Mh.Item(0).submatches(0)ElseRegGet = ""End IfSet Regex = Nothing
End Function
Sub 自动编号转文本()If Selection.Type = wdSelectionIP ThenActiveDocument.Content.ListFormat.ConvertNumbersToTextActiveDocument.Content.Find.Execute FindText:="^t", replacewith:=" ", Replace:=wdReplaceAllElseSelection.Range.ListFormat.ConvertNumbersToTextSelection.Find.Execute FindText:="^t", replacewith:=" ", Replace:=wdReplaceAllEnd If
End Sub
转载于:https://www.cnblogs.com/nextseven/p/7489255.html
20170907wdVBA_GetCellsContentToExcel相关推荐
最新文章
- Bootstrap验证控件的使用
- google 浏览器清除缓存
- 阿里SRE体系如何支撑24小时峰值压力、220+个国家“剁手党”?
- POJ2084 Game of Connections(数学,dp)
- RHEL 6.0安装Qt
- druid加密mysql_Druid 数据库用户密码加密 代码实现
- mysql基本介绍和优化技巧
- mysql mycat 路由规则_Mycat水平拆分之十种分片规则
- oracle生成一维码,一维码生成软件下载-一维码生成器v8.1 安装版 - 极光下载站
- H5游戏《守塔兵团》你必须要知道的4件事
- UWA学堂|逻辑代码模块
- python爬斗鱼直播房间名和主播名_谁才是斗鱼一哥?(用Python抓取斗鱼直播间信息)...
- 人生何尝不是一盘“大富翁”呢
- php如何将excel数据导入到数据库,【PHP】将Excel数据导入到MySQL数据库中
- mybatis 级联查询
- DWARF调试格式的简介
- Android11系统丨RK3568开发板运行第一个Android程序
- 安装和控制DNS服务器
- 微信小程序——选择图片/拍照
- 银行联行号-联行号api接口-联行号数据源
热门文章
- LINQ 学习路程 -- 查询语法 LINQ Query Syntax
- C/C++中无条件花括号的妙用
- 输入焦点默认指示在编辑框上
- Linux Socket API Connect 函数详解
- 常用正则表达式(?i)忽略字母的大小写!
- Opencv图像保存到电脑及显示
- java 快速构建ssm项目_SSM快速搭建
- java 数组 内存_图解Java数组的内存分配
- Python应用实战-如何用Pyecharts绘制可视化地图?
- ios framework 找不到.h_找不到好看的壁纸?上万张「高清壁纸」,都在iOS捷径里...