【发布时间】:2013-10-11 08:30:20
【问题描述】:
我遇到了下面的代码,它搜索打开的 word 文档并在文档的所有区域 (StoryRanges) 内执行查找和替换。 它工作正常,但是我想问我如何修改此代码以查看所选文件夹中的所有文档并对该文件夹中的所有文档执行查找和替换?,而不仅仅是打开的活动文档?
我的计划是将宏分配给 Excel 中的一个按钮,以便用户可以单击该按钮,导航到该文件夹并立即对大量文档进行查找和替换。
我可以修改“IN ActiveDocument.StoryRanges”部分来查看文件夹吗?我不确定我可以修改它。顺便说一句...我是 vba 的新手,我正在努力研究和学习...我非常感谢您的时间、耐心和您在我试图找到自己的脚时可以提供的任何帮助 - 亚历克斯。
将 myStoryRange 调暗为范围
For Each myStoryRange In ActiveDocument.StoryRanges
With myStoryRange.Find
.Text = "Text to find to replace goes here"
.Replacement.Text = "And the replacement text goes here"
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Do While Not (myStoryRange.NextStoryRange Is Nothing)
Set myStoryRange = myStoryRange.NextStoryRange
With myStoryRange.Find
.Text = "Text to find to replace goes here"
.Replacement.Text = "And the replacement text goes here"
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Loop
Next myStoryRange
【问题讨论】:
-
通过使用
DIR在 SO 或 Google 上进行搜索。你会发现很多例子。 -
让我知道你是否仍然被卡住,我们会从那里拿走它:)
-
感谢您的回复,我一直在研究DIR,也遇到了FileSystemObjects。我仍在思考并试图弄清楚我应该如何/&采用哪种方法。非常感谢