批次尋找和取代 - Microsoft Word 2013

批次尋找和取代 - Microsoft Word 2013

我正在嘗試建立一個巨集,以便在多個 Word 文件中進行批次查找和替換。我在網路上找到了這個檔案並對其進行了更改,但是我不斷收到運行時錯誤(5174),指出無法找到該檔案(即使它肯定在資料夾中)。

此外,在找到最初問題的解決方案後,我需要能夠找到並替換頁腳中的圖片。

Sub ReplaceText()
  Dim Directory As String
  Dim FType As String
  Dim FName As String

Directory = "C:\Users\pieria\Desktop\TempPics"
FType = "*.docx"

ChDir Directory
FName = Dir(FType)
' for each file you find, run this loop
Do While FName <> ""
    ' open the file
    Documents.Open FileName:=FName  '<--Error is supposedly here 

    ' search and replace the company name
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "CompanyA"
        .MatchCase = True
        .Replacement.Text = "CompanyB"
    End With
    Selection.Find.Execute Replace:=wdReplaceAll

    ' save and close the current document
    ActiveDocument.Close wdSaveChanges

    ' look for next matching file
    FName = Dir
Loop 
End Sub

答案1

對我來說效果很好。我的猜測是您的輸入檔損壞和/或檔名不穩定。

開始調試時間:

在 VBA 編輯器中,設定斷點Documents.Open FileName:=FName線,並且新增手錶Fname

運行程式碼,每次停止時,記下它正在處理的檔案名稱(顯示在「監視」窗格中)。現在,當它拋出錯誤時,您就會知道哪個文件有問題。

檢查該檔案是否有損壞、權限問題和/或一般異常。 :)

休息和觀看

答案2

這是一個可能的答案,它被設計為用戶友好的:

Public Sub MassReplace()

Dim strPath As String
 Dim strFile As String
Dim FType As String
Dim FName As String
Dim strFind As String
 Dim strReplace As String
 Dim WordApp As Object
 Dim WordDoc As Object

'上面的文字定義了你的對象

 strFind = InputBox("Enter Text to find") 

 strReplace = InputBox("Enter replacement Text") 

' 使用者使用輸入框定義他們想要尋找和替換的文本

    With Application.FileDialog(msoFileDialogFolderPicker)
    If .Show Then
     strPath = .SelectedItems(1)
      Else
         MsgBox "No folder selected!", vbExclamation
         Exit Sub
        End If
 End With

  If Right(strPath, 1) <> "\" Then
     strPath = strPath & "\"
     strFile = Dir(strPath & "*.docx*")
 End If
 Application.ScreenUpdating = False

'上面的程式碼區塊允許使用者選擇要搜尋的資料夾文件

 Do While strFile <> "" 'Do this while strFile is not blank

    Set WordApp = CreateObject("Word.Application") 'Open MS word
    WordApp.Visible = True 'Make word visible
    Set WordDoc = WordApp.Documents.Open(strPath & strFile) 'open file in folder
    WordApp.ActiveDocument.Range.Select ' select all text

    With WordApp.Selection.Find 'Using the find function allows a search of text
    .Text = strFind 'find "strFind"
    .Replacement.Text = strReplace 'replacement text is "strReplace"
    .Wrap = wdFindContinue
    '.Format = False
    '.MatchCase = False
    '.MatchWholeWord = False
    '.MatchWildcards = False
    '.MatchSoundsLike = False
    .Execute Replace:=wdReplaceAll 'replace all text

    WordApp.ActiveDocument.Close wdSaveChanges 'Close document and save changes

    End With 'End with block



    WordApp.Quit 'Close the word application
    strFile = Dir 'Go back to the directory

   Loop

 Application.ScreenUpdating = True
 End Sub

這似乎適用於 Word 2016。要替換數字而不是文本,請將 strFind 和 strReplace 定義為整數(或其他數字類型)而不是文字。快樂編碼!

相關內容