如何搜索和替换多个Word文件?


10

我正在寻找一种方法来搜索和替换许多Word(.doc)文件(以实现过程自动化)。

到目前为止,我找到的软件仅允许我搜索,而不能替换。

如何使用Word做到这一点?

Answers:


7

您可以尝试使用VBA查找和替换(缓存的链接)。

VBA查找和替换©提供了一种使用单个用户定义的“查找”和“替换”变量对或用户定义的“查找”和“替换”列表来查找和替换文档(或文档集合)中任意位置的文本的方法。对。它还提供了一种查找文本并将查找到的文本替换为用户定义的“ AutoText”或“ Building Block”条目的方法。


看起来就像我想要的-摇滚!
塔尔·加利利

该插件搜索哪些文件?我找不到指定起始目录的可能性。它仅使用所有打开的文件吗?以及如何搜索而不替换呢?
Michael S.

2
@gentlesea的“处理批处理文件夹中的文件”设置没有帮助吗?
杰伊·威克

1
该URL的末尾还有一个额外的时间–我将其删除,但是StackOverflow的设计不正确,并防止了该情况:gregmaxey.mvps.org/VBA_Find_And_Replace.htm
Chris Adams

1
谢谢,非常感谢。(:
p._phidot_

0

为了在给定根文件夹下的多个文件夹中的多个MS Word文件中执行多个常规和基于通配符的替换,我创建了以下VBA宏。为了使用它,您需要更改以下变量(常量)的内容:

  • rootPath:根文件夹,位于其中的包含Word文档的文件夹。
  • findTextsWildreplaceTextsWild:基于通配符的查找和替换表达式的数组。
  • findTextsreplaceTexts:普通查找和替换表达式的数组。

也许您会发现它有用:-)

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



-1

Notepad ++可以使用“在文件中查找”来执行此操作


键盘快捷键Ctrl + Shift + F
Yitzchak 2014年

2
Notepad ++可以打开Microsoft Word文档吗?
丹尼尔·贝克

1
如果字符串匹配和替换字符串不是特殊字符,则此方法可以工作,因为Word 2007+文档本质上实际上是XML文件。但是我说可能。在发布答案之前,您是否想测试一下。
tumchaaditya 2014年

-2
Option Explicit

Public Sub BatchReplaceAll()
    Dim FirstLoop As Boolean
    Dim myFile As String
    Dim PathToUse As String
    Dim myDoc As Document
    Dim Response As Long

    PathToUse = ActiveDocument.Path
    On Error Resume Next
    Documents.Close SaveChanges:=wdPromptToSaveChanges
    FirstLoop = True
    myFile = Dir$(PathToUse & "\*.doc*")

    While myFile <> ""
        Set myDoc = Documents.Open(PathToUse & "\" & myFile)
        If FirstLoop Then
            Application.Dialogs(wdDialogEditReplace).Show
            FirstLoop = False

            Response = MsgBox("Do you want to process the rest of the files in this folder", vbYesNo)
            If Response = vbNo Then Exit Sub
        Else
            With Dialogs(wdDialogEditReplace)
                .ReplaceAll = 1
                .Execute
            End With
        End If
        myDoc.Close SaveChanges:=wdSaveChanges
        myFile = Dir$()
    Wend
End Sub

3
此代码块是什么,它做什么,如何帮助解决该问题?
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.