我正在寻找一种在许多 Word (.doc) 文件中进行搜索和替换的方法(为了实现该过程的自动化)。
我目前找到的软件只允许我搜索,但不允许替换。
我怎样才能使用 Word 做到这一点?
答案1
你可以尝试VBA 查找和替换(缓存链接)。
VBA Find and Replace© 提供了一种使用单个用户定义的“查找”和“替换”变量对或用户定义的“查找”和“替换”对列表在文档(或文档集合)中任意位置查找和替换文本的方法。它还提供了一种使用用户定义的“自动图文集”或“构建块”条目查找文本并替换找到的文本的方法。
答案2
为了在给定根文件夹下的多个文件夹中的多个 MS Word 文件中执行多个常规和基于通配符的替换,我创建了以下 VBA 宏。为了使用它,您必须更改以下变量(常量)的内容:
- 根路径:根文件夹,其中包含 Word 文档的文件夹。
- 查找文本野生和替换文本:基于通配符的查找和替换表达式数组。
- 查找文本和替换文本:正常查找和替换表达式的数组。
也许你会发现它很有用:-)
Sub GlobalTextReplacement()
' Root under which all manuals are stored
Dim rootPath As String
rootPath = "c:\Data\Manuals\"
' Find and replace text for wildcard replacement. Performed first.
Dim findTextsWild() As Variant, replaceTextsWild() As Variant
findTextsWild = Array("[ ]{2;}", "[cC]onfiguration[/ ]@[pP]olicy [rR]epository", "[sS]ervlet[- ]@[fF]ilter")
replaceTextsWild = Array(" ", "Configuration/Policy Repository", "Servlet-Filter")
' Find and replace text for normal case insensitive replacement. Performed second.
Dim findTexts() As Variant, replaceTexts() As Variant
findTexts = Array("DirX Access", "Policy Repository", "User Repository", "Servlet", "servletfilter", "SAML assertion", "DirX Access Server", "DirX Access Manager", "Deployment Manager", "Policy Manager", "Client SDK", "^p ", " ^p")
replaceTexts = Array("DirX Access", "Policy Repository", "User Repository", "Servlet", "Servlet-Filter", "SAML assertion", "DirX Access Server", "DirX Access Manager", "Deployment Manager", "Policy Manager", "Client SDK", "^p", "^p")
' Main code
Application.ScreenUpdating = False
Dim dirNames(20) As String
Dim dirNamesCount As Integer
dirNamesCount = 0
Dim dirName As String
dirName = Dir$(rootPath & "*", vbDirectory)
Do Until LenB(dirName) = 0
Dim dirPath As String
dirPath = rootPath & dirName
If ((GetAttr(dirPath) And vbDirectory) = vbDirectory) And (dirName <> ".") And (dirName <> "..") Then
dirNamesCount = dirNamesCount + 1
dirNames(dirNamesCount) = dirPath & "\"
End If
dirName = Dir$
Loop
Do While dirNamesCount > 0
Dim fileName As String
dirName = dirNames(dirNamesCount)
dirNamesCount = dirNamesCount - 1
fileName = Dir$(dirName & "*.doc", vbDirectory)
Do Until LenB(fileName) = 0
Dim filePath As String
filePath = dirName & fileName
fileName = Dir$
Dim document As document
Set document = Documents.Open(filePath)
document.TrackRevisions = True
document.Select
Dim i As Integer, maxIndex As Integer
maxIndex = UBound(findTextsWild)
For i = LBound(findTextsWild) To maxIndex
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = findTextsWild(i)
.Replacement.Text = replaceTextsWild(i)
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue, MatchWildcards:=True
End With
Next
maxIndex = UBound(findTexts)
For i = LBound(findTexts) To maxIndex
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = findTexts(i)
.Replacement.Text = replaceTexts(i)
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue, MatchCase:=False, MatchWildcards:=False
End With
Next
document.Save
document.Close
Loop
Loop
Application.ScreenUpdating = True
End Sub
答案3
难道它不能在 Microsoft Word 本身中完成吗?
它位于“编辑”菜单(Word 2003)或“主页”选项卡/编辑部分(Word 2007)中
答案4
Notepad++ 可以使用“在文件中查找”来执行此操作