ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 639|回复: 1

WORD关键词删除

[复制链接]

TA的精华主题

TA的得分主题

发表于 2019-8-17 21:07 | 显示全部楼层 |阅读模式
请哪位大咖帮一帮忙,我有一批WORD文档,有doc格式和docx格式的,请哪位大师能帮我把如果在第二至第五段有某关键词(这关键词有好几个:比如校长,班主任,老师等),则关键词所在的行或关键词所在的段落删除,关键词在其他地方不改变

TA的精华主题

TA的得分主题

发表于 2019-8-18 00:20 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
* 楼主,请做好原文档备份后试用下面的宏:(注意:不要先打开文档,要先运行宏。请核对是否正确。)
  1. Sub DIR_循环遍历文件夹_删除关键词()

  2.     On Error Resume Next
  3.     Dim arr, a As Paragraph, j&, r As Range
  4.     arr = Array("校长", "班主任", "老师")
  5.     Dim objShell As Object, objFolder As Object, SearchPath$, DicList As Object, FileList As Object, Key, NowDic$, NowFile$, i&, FileName, FilePath, doc As Document, x&

  6.     Set objShell = CreateObject("Shell.Application")
  7.     Set objFolder = objShell.BrowseForFolder(0, "请选择文件夹!", 0, 0)

  8.     SearchPath = objFolder.self.Path & ""

  9.     Set objShell = Nothing
  10.     Set objFolder = Nothing

  11.     If MsgBox("请确认!是否处理文件夹 " & SearchPath & " ?", 4 + 16) = vbNo Then Exit Sub

  12.     Set DicList = CreateObject("Scripting.Dictionary")
  13.     Set FileList = CreateObject("Scripting.Dictionary")

  14.     DicList.Add SearchPath, ""

  15.     i = 0
  16.     Do While i < DicList.Count
  17.         Key = DicList.keys
  18.         NowDic = Dir(Key(i), vbDirectory)
  19.         Do While NowDic <> ""
  20.             If (NowDic <> ".") And (NowDic <> "..") Then
  21.                 If (GetAttr(Key(i) & NowDic) And vbDirectory) = vbDirectory Then DicList.Add Key(i) & NowDic & "", ""
  22.             End If
  23.             NowDic = Dir()
  24.         Loop
  25.         i = i + 1
  26.     Loop

  27.     For Each Key In DicList.keys
  28.         NowFile = Dir(Key)
  29.         Do While NowFile <> ""
  30.             FileList.Add NowFile, Key
  31.             NowFile = Dir()
  32.         Loop
  33.     Next

  34.     i = 0
  35.     FileName = FileList.keys
  36.     FilePath = FileList.Items
  37.     Do While i < FileList.Count
  38.         If FilePath(i) & FileName(i) Like "*.doc*" Then
  39.             Set doc = Documents.Open(FileName:=FilePath(i) & FileName(i), Visible:=True)
  40. '
  41.             With doc
  42.                 If .Paragraphs.Count >= 5 Then
  43.                 Set r = .Range(Start:=.Paragraphs(2).Range.Start, End:=.Paragraphs(5).Range.End)
  44.                     For j = 0 To UBound(arr)
  45.                         For Each a In r.Paragraphs
  46.                             If Len(r) = 0 Then Exit For
  47.                             With a.Range
  48.                                 If Not .Information(12) Then
  49.                                     If .Text Like "*" & arr(j) & "*" Then .Delete
  50.                                 End If
  51.                             End With
  52.                         Next
  53.                     Next j
  54.                 End If
  55.             End With
  56. '
  57.             doc.Close SaveChanges:=wdSaveChanges
  58.             x = x + 1
  59.         End If
  60.         i = i + 1
  61.     Loop

  62.     Set DicList = Nothing
  63.     Set FileList = Nothing

  64.     MsgBox "文件夹包含 " & i & " 个文件!" & vbCr & "共处理 Word 文档(*.docx/*.doc) " & x & " 个!", 0 + 48
  65. End Sub
复制代码
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

最新热点上一条 /1 下一条

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-26 22:29 , Processed in 0.026904 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表