Tôi đang tìm cách để tìm kiếm và thay thế trong nhiều tệp Word (.doc) (cho mục đích tự động hóa quy trình).
Phần mềm tôi tìm thấy cho đến nay chỉ cho phép tôi tìm kiếm, nhưng không thay thế.
Làm thế nào tôi có thể làm điều đó với Word?
Tôi đang tìm cách để tìm kiếm và thay thế trong nhiều tệp Word (.doc) (cho mục đích tự động hóa quy trình).
Phần mềm tôi tìm thấy cho đến nay chỉ cho phép tôi tìm kiếm, nhưng không thay thế.
Làm thế nào tôi có thể làm điều đó với Word?
Câu trả lời:
Bạn có thể thử VBA Find & Thay thế (liên kết lưu trữ).
Tìm và thay thế VBA © cung cấp phương pháp tìm và thay thế văn bản ở bất kỳ đâu trong tài liệu (hoặc bộ sưu tập tài liệu) bằng cách sử dụng các cặp biến "tìm" và "thay thế" do người dùng xác định hoặc danh sách do người dùng xác định là "tìm" và "thay thế" cặp. Nó cũng cung cấp một phương pháp để tìm văn bản và thay thế văn bản tìm thấy bằng mục nhập "AutoText" hoặc "Building Block" do người dùng xác định.
Với mục đích thực hiện nhiều thay thế dựa trên ký tự bình thường và ký tự đại diện trong nhiều tệp MS Word nằm trong nhiều thư mục trong thư mục gốc đã cho, tôi đã tạo theo macro VBA. Để sử dụng nó, bạn phải thay đổi nội dung của các biến (hằng) sau:
Có thể bạn sẽ tìm thấy nó hữu ích :-)
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
              Nó có thể được thực hiện trong chính Microsoft Word không?
Nó nằm trong menu Chỉnh sửa của bạn (Word 2003) hoặc tab Trang chủ / Phần chỉnh sửa (Word 2007)
Nếu bạn không sở hữu Microsoft Word và đang tìm kiếm một giải pháp thay thế miễn phí sẽ giúp bạn, hãy thử OpenOffice.org .
Tôi không nghĩ rằng điều này sẽ giúp bạn nếu bạn đang tìm cách tự động hóa quá trình này.
Notepad ++ Có thể thực hiện việc này bằng cách sử dụng "Tìm trong tệp"
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