VBA收集Word关键字批量处理
批量对关键字打标记(文件夹遍历)Option Explicit Private Const FINISHED_FILE_PATH As String = "newData" " 存完成文件的目录名 Private Const ERROR_FILE_PATH As String = "errorData" " 存出错文件的目录名 Private Const SKIP_FILE_PATH As String = "skipData" " 存跳过文件的目录名 Private Const ERROR_FILE_SUFFIX As String = "Err.log" " 出错日志后缀 Private Const SKIP_FILE_SUFFIX As String = "Skip.log" " 跳过日志后缀 Dim fs As Object " 文件系统对象 Dim errLogFile As String " 错误日志 Dim skipLogFile As String " 跳过记录的日志 Dim sourceFilePath As String " 选择要处理的文件所在 Sub 遍历文件夹中对文档的关键字打标记() On Error GoTo ErrorHandler Dim CurrPath$, CurrFile$, currDoc As Document, keyArray() As String, fileNameExtension As String, newPath As String, skipPath As String, errPath As String, tempFileName As String " --------- 初始化 开始 ---------- " 选择要处理的文件所在 If Not SelectFolder() Then Exit Sub If MsgBox("要处理的文件在:" & sourceFilePath, vbYesNo + vbInformation, "确认源文件目录") <> vbYes Then Exit Sub CurrPath = ThisDocument.path & "" errLogFile = CurrPath & Replace(ThisDocument.Name, ".docm", ERROR_FILE_SUFFIX) skipLogFile = CurrPath & Replace(ThisDocument.Name, ".docm", SKIP_FILE_SUFFIX) " 准备文件夹 newPath = CurrPath & FINISHED_FILE_PATH skipPath = CurrPath & SKIP_FILE_PATH errPath = CurrPath & ERROR_FILE_PATH If Dir(newPath, vbDirectory) = vbNullString Then MkDir newPath If Dir(skipPath, vbDirectory) = vbNullString Then MkDir skipPath If Dir(errPath, vbDirectory) = vbNullString Then MkDir errPath " 初始化文件系统对象 Set fs = CreateObject("Scripting.FileSystemObject") " --------- 初始化 结束 ---------- CurrFile = Dir(sourceFilePath) Do Until CurrFile = "" If CurrFile <> ThisDocument.Name And (Right(CurrFile, 5) = ".docx" Or Right(CurrFile, 4) = ".doc") Then tempFileName = sourceFilePath & CurrFile Set currDoc = Documents.Open(tempFileName, Visible:=False) " 找到关键字的,另存一份到 newPath 下 If 对关键字打标记(currDoc, ThisDocument) Then currDoc.SaveAs2 FileName:=newPath & CurrFile, FileFormat:=wdFormatXMLDocument Kill tempFileName currDoc.Close wdDoNotSaveChanges Set currDoc = Nothing "DoEvents Else currDoc.Close wdDoNotSaveChanges Set currDoc = Nothing skiplog tempFileName Call moveFile(tempFileName, skipPath & CurrFile) End If End If NextFile: CurrFile = Dir() Loop Set fs = Nothing Call MsgBox("处理完毕", vbOKOnly + vbInformation, "温馨提示") Exit Sub ErrorHandler: errlog "================================================================================" errlog "【错误文件】" & tempFileName errlog Err.Number & ":" & Replace(Err.Description, vbLf, " vbCrLf ") Call moveFile(tempFileName, errPath & CurrFile) Resume NextFile End Sub 写日志" 写日志 Sub errlog(logMsg As String) Shell "cmd.exe /c echo " & Format(Now, "YYYY-MM-DD HH:MM:SS") & " ===》 " & logMsg & " >> " & errLogFile End Sub Sub skiplog(logMsg As String) Shell "cmd.exe /c echo " & logMsg & " >> " & skipLogFile End Sub 移动文件" 移动文件 Sub moveFile(sourcePath As String, targetPath As String) On Error GoTo ErrorHandler Call fs.moveFile(sourcePath, targetPath) Error_Handler_Exit: Exit Sub Exit Sub ErrorHandler: errlog "================================================================================" errlog "【移动文件失败】" & sourcePath errlog Err.Number & ":" & Replace(Err.Description, vbLf, " vbCrLf ") Resume Error_Handler_Exit End Sub 选择目录Function SelectFolder() With Application.FileDialog(msoFileDialogFolderPicker) .InitialFileName = ThisDocument.path & "" If .Show = -1 Then " OK返回 -1,Cancel 返回 0 sourceFilePath = .SelectedItems(1) & "" SelectFolder = True Else SelectFolder = False End If End With End Function 对关键字打标记(查找替换)
注意:如果是 .Execute Replace:=wdReplaceOne (替换第一个), 那么 With doc.Content.Find 就要放到循环里面了。否则会出现意外丢失的情况! :比如: ABA 先搜 B ,再搜 A ,替换的不是第一个 A 用是第二个 A 。未仔细研究根源,仅此备注。 Function 对关键字打标记(doc As Document, MainDoc As Document) Dim i As Integer, keyArrLen As Integer, keyArray() As String, styleName As String, edited As Boolean edited = False " 默认未编辑状态 keyArray = 获取关键字(MainDoc) keyArrLen = UBound(keyArray) styleName = 创建样式(doc) With doc.Content.Find .ClearFormatting .Replacement.ClearFormatting .Replacement.style = styleName .Replacement.Text = "^&" .Forward = True .Wrap = wdFindContinue " 遍历查找关键字,并标示 For i = 0 To keyArrLen .Text = keyArray(i) .Execute Replace:=wdReplaceAll " 找到了关键字,标记为编辑过。 If .Found Then edited = True End If Next End With 对关键字打标记 = edited End Function 创建样式Function 创建样式(doc As Document) On Error Resume Next " 出错时忽略,继续向下运行。 " 判断样式,不存在则创建 Dim flag As Boolean, syte As style, styleName As String styleName = "关键字" flag = True For Each syte In doc.Styles If syte.NameLocal = styleName Then flag = False End If Next If flag Then doc.Styles.Add Name:=styleName, Type:=wdStyleTypeCharacter With doc.Styles(styleName).Font .NameFarEast = "微软雅黑" .Bold = True .Color = wdColorYellow .Shading.ForegroundPatternColor = wdColorAutomatic .Shading.BackgroundPatternColor = wdColorRed End With End If 创建样式 = styleName End Function 获取关键字(动态数组)Function 获取关键字(doc As Document) Dim keyArray() As String, arrLen As Integer, pgs As Paragraphs, i As Integer " 取当前文档所有段落 Set pgs = doc.Paragraphs arrLen = pgs.Count - 1 " 重置动态数组的长度 ReDim keyArray(arrLen) As String " 遍历段落,将文字加入数组 For i = 0 To arrLen keyArray(i) = Replace(Trim(pgs(i + 1).Range.Text), vbCr, "") Next 获取关键字 = keyArray End Function 参考资料
Excel、Word VBA 学习笔记_笑虾的博客-CSDN博客
VBA操作文件和文件夹步骤_w3cschool
VBA 收集 Word关键字批量处理-Excel版_笑虾的博客-CSDN博客_vba word 查找关键字
华为Mate30Pro电影四摄再次深耕影像,再创不可能10月伊始,最受瞩目的除了阅兵式,应该就是国庆档上映的献礼大片我和我的祖国。而在这个让人动容的电影,片尾MV出现的由华为Mate30Pro拍摄的视频片段,引起了众多观众的注意,很多
为什么同事总能步步高升?原因竟然是他选择了一汽大众新迈腾在当今社会处处充满竞争,如果你落后一步就会步步落后。同样在汽车行业也是如此,但是一汽大众却能始终引领行业潮流,特别其旗下的新迈腾这款车型在商务车中更是标杆的存在。如果你也想在你的行
同盾科技蒋韬心存敬畏,向善而行同盾科技CEO蒋韬发布公开信,侧面回应了一些关于同盾科技的谣言,也表达了他对于数据分析与应用行业未来发展的认知。近日,多家大数据风控服务提供商传出高管协助调查或公司查封爬虫业务终止
网红JEET蓝牙耳机凭什么走红运动圈,引发疯狂种草?最近,JEET蓝牙耳机在运动圈内爆火,众多运动达人们纷纷疯狂种草。而品牌推出的顶配级旗舰机皇更是被用户冠以断货王之称!既不是国际大牌,也没有明星代言,更没有大肆广告炒作博眼球,那么
国米乔府大院新品上市发布会圆满完成五常大米产业发展路途中,五常市人民政府从未停止探索与努力。从1993年提出绿色战略口号到2015年通过三确一检一码建立起五常大米全产业链可追溯体系再到2019年与金融大数据等平台合
势头太猛!沃尔沃全新S60现身多次就卖20多万近日,最近沃尔沃全新S60这款车的关注度很高,那么我们就来简单介绍一下,新车有丰富的配置,助力年轻一代更好地出行。这次沃尔沃全新S60搭载斯堪的纳维亚设计,虽然并未对前脸部分的造型
历史性一刻!海宁杭海新城纳入杭州钱塘新区战略规划范围2019年10月23日,海宁迎来历史性一刻!杭州钱塘新区与海宁市政府签署了全面战略合作协议,海宁杭海新区纳入钱塘新区战略规划范围。由此开始,杭州钱塘新区和海宁市将共同打造省跨行政区
说实话,我愿意把这个低调的神器安利给全世界给你们说个好玩的,去看个电影,把结束时间记成入场时间了,我特么本来心情就有点不太美丽,电影院又全是一对一对的,心情就更糟糕了。还不如回家看电视。正好红米70寸价格屠夫发布了,等开售
看完这几点,你说沃尔沃性价比高不高?在国内汽车市场中,以有气派有面子空间大等特点的车型层出不穷。但是随着汽车消费的多元化,在豪华车车领域沃尔沃会是众多消费者的首选车型。除了造型设计空间舒适与动力性能这样的基本要素外,
新迈腾成为商务人士们心中的佳选车型,靠的就是实力新迈腾作为市场中B级车的最新标杆之一,如今以在广州车展上出展。新迈腾不仅在外观上较上一版本有了很大的改观,在内饰和配置上也进行了大的变化。如今的新迈腾处处体现着他的运动风格和商务风
这是真的!百公里油耗6。6新迈腾绿水青山就是金山银山!时代的浪潮向前推进,低碳出行顺应时代,新迈腾当然义不容辞。充分响应时代号召,与时代发展紧紧相连,强大动力组合的实力超越其它商务车型,全新设计的变速箱匹配不同功