ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

[求助] 遍历word文档出现死循环

[复制链接]

TA的精华主题

TA的得分主题

发表于 2020-5-28 11:50 | 显示全部楼层 |阅读模式
本帖最后由 Yorphone 于 2020-5-28 12:02 编辑

在遍历文档时出现死循环,我的文件夹里面有60个word的docx文档,
不知道能不能加一下跳出的机制,

麻烦各位大神啦,
非常感谢,先行谢过~

  1. Sub a()
  2.     Dim doc As Document, myFile As String
  3.     Dim a As Range
  4.    
  5.     myFile = Dir("D:" & "*.docx")
  6.    
  7.     Do While myFile <> ""
  8.         myFile = "D:" & myFile
  9.         Set doc = Documents.Open(myFile)
  10.         Set a = doc.Range


  11.         Selection.WholeStory
  12.         '展开域
  13.         Selection.Fields.ToggleShowCodes
  14.         '≥≤ 把大换小,把小换小
  15.         a.Find.Execute FindText:="≤", MatchWildcards:=True, replacewith:="$", Replace:=wdReplaceAll
  16.         a.Find.Execute FindText:="≥", MatchWildcards:=True, replacewith:="#", Replace:=wdReplaceAll
  17.         a.Find.Execute FindText:="$", MatchWildcards:=True, replacewith:="≥", Replace:=wdReplaceAll
  18.         a.Find.Execute FindText:="#", MatchWildcards:=True, replacewith:="≤", Replace:=wdReplaceAll
  19.         Selection.WholeStory
  20.         Selection.Fields.ToggleShowCodes
  21.         
  22.         '保存退出
  23.         doc.Save
  24.         doc.Close
  25.         
  26.         Set doc = Nothing
  27.         
  28.         '查找下一个
  29.         myFile = Dir
  30.     Loop
  31. End Sub
复制代码



TA的精华主题

TA的得分主题

 楼主| 发表于 2020-5-28 11:51 | 显示全部楼层
本帖最后由 Yorphone 于 2020-5-28 12:03 编辑

希望大神们能指点指点~

TA的精华主题

TA的得分主题

发表于 2020-5-28 12:07 来自手机 | 显示全部楼层
楼主是需要批量替换吗?这种贴子,以前回过贴的,搜搜看

TA的精华主题

TA的得分主题

 楼主| 发表于 2020-5-29 14:44 | 显示全部楼层
3190496160 发表于 2020-5-28 12:07
楼主是需要批量替换吗?这种贴子,以前回过贴的,搜搜看

不是的,我是写了很长一段自动排版论文的代码,现在是要自动打开那60个文档,并应用、保存和关闭~

TA的精华主题

TA的得分主题

发表于 2020-6-1 23:35 | 显示全部楼层
请楼主参考:
  1. Sub DIR_循环遍历文件夹()

  2.     On Error Resume Next

  3.     Dim objShell As Object, objFolder As Object, SearchPath$, DicList As Object, FileList As Object, Key, NowDic$, NowFile$, i&, FileName, FilePath, doc As Document, x&

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

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

  7.     Set objShell = Nothing
  8.     Set objFolder = Nothing

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

  10.     Set DicList = CreateObject("Scripting.Dictionary")
  11.     Set FileList = CreateObject("Scripting.Dictionary")

  12.     DicList.Add SearchPath, ""

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

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

  32.     i = 0
  33.     FileName = FileList.keys
  34.     FilePath = FileList.Items
  35.     Do While i < FileList.Count
  36.         If FilePath(i) & FileName(i) Like "*.doc*" Then
  37.             Set doc = Documents.Open(FileName:=FilePath(i) & FileName(i), Visible:=False)
  38.             doc.Content.Font.Color = wdColorRed '单个文档处理
  39.             doc.Close savechanges:=wdSaveChanges
  40.             x = x + 1
  41.         End If
  42.         i = i + 1
  43.     Loop

  44.     Set DicList = Nothing
  45.     Set FileList = Nothing

  46.     MsgBox "文件夹包含 " & i & " 个文件!" & vbCr & "共处理 Word 文档(*.docx/*.doc) " & x & " 个!", 0 + 48
  47. End Sub
  48. Sub FSO_循环遍历文件夹()

  49.     On Error Resume Next

  50.     Dim objShell As Object, objFolder As Object, pPath$, f As Object, fd As Object, fso As Object, Stack$(), top&, n&, stxt$, doc As Document, x&

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

  53.     pPath = objFolder.self.Path & ""

  54.     Set objShell = Nothing
  55.     Set objFolder = Nothing

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

  57.     Set fso = CreateObject("Scripting.FileSystemObject")

  58.     top = 1
  59.     ReDim Stack(0 To top)

  60.     Do While top >= 1
  61.         For Each f In fso.GetFolder(pPath).Files
  62.             n = n + 1
  63.             stxt = f.Path
  64.             If stxt Like "*.doc*" Then
  65.                 Set doc = Documents.Open(FileName:=stxt, Visible:=False)
  66.                 doc.Content.Font.Color = wdColorRed '单个文档处理
  67.                 doc.Close savechanges:=wdSaveChanges
  68.                 x = x + 1
  69.             End If
  70.         Next
  71.         For Each fd In fso.GetFolder(pPath).SubFolders
  72.             Stack(top) = fd.Path
  73.             top = top + 1
  74.             If top > UBound(Stack) Then ReDim Preserve Stack(0 To top)
  75.         Next
  76.         If top > 0 Then pPath = Stack(top - 1): top = top - 1
  77.     Loop

  78.     Set f = Nothing
  79.     Set fd = Nothing
  80.     Set fso = Nothing

  81.     MsgBox "文件夹包含 " & n & " 个文件!" & vbCr & "共处理 Word 文档(*.docx/*.doc) " & x & " 个!", 0 + 48
  82. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2020-6-1 23:38 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
* 楼主,在处理论文前建议将文档打包检测并备份,以免排版不满意而误保存受损。
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

关闭

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

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

GMT+8, 2024-4-20 08:47 , Processed in 0.042891 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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