จะค้นหาและแทนที่ข้อความหลายรายการใน Word ด้วย Excel ได้อย่างไร?
ผู้เขียนXiaoyang•แก้ไขล่าสุด
ใน Microsoft Word คุณสมบัติค้นหาและแทนที่เป็นวิธีที่มีประสิทธิภาพในการค้นหาและแทนที่ข้อความเฉพาะอย่างรวดเร็ว อย่างไรก็ตาม เมื่อคุณต้องการแทนที่คำที่แตกต่างกันหลายคำ การป้อนแต่ละคำด้วยตนเองอาจใช้เวลานาน
โค้ด VBA: ค้นหาและแทนที่ข้อความหลายรายการในไฟล์ Word หนึ่งไฟล์
Sub replace_texts_range_of_cells()
'Updateby ExtendOffice
Dim xWordApp As Word.Application
Dim xDoc As Word.Document
Dim xRng As Range
Dim I As Integer
Dim xFileDlg As FileDialog
On Error GoTo ExitSub
Set xFileDlg = Application.FileDialog(msoFileDialogFilePicker)
xFileDlg.AllowMultiSelect = False
xFileDlg.Filters.Add "Word Document", "*.docx; *.doc; *.docm"
xFileDlg.FilterIndex = 2
If xFileDlg.Show <> -1 Then GoTo ExitSub
Set xRng = Application.InputBox("Please select the lists of find and replace texts (Press Ctrl key to select two same size ranges):", "Kutools for Excel", , , , , , 8)
If xRng.Areas.Count <> 2 Then
MsgBox "Please select two columns (press Ctrl key), the two ranges have the same size.", vbInformation + vbOKOnly, "Kutools for Excel"
GoTo ExitSub
End If
If (xRng.Areas.Item(1).Rows.Count <> xRng.Areas.Item(2).Rows.Count) Or _
(xRng.Areas.Item(1).Columns.Count <> xRng.Areas.Item(2).Columns.Count) Then
MsgBox "Please select two columns (press Ctrl key), the two ranges have the same size.", vbInformation + vbOKOnly, "Kutools for Excel"
GoTo ExitSub
End If
Set xWordApp = CreateObject("Word.application")
xWordApp.Visible = True
Set xDoc = xWordApp.Documents.Open(xFileDlg.SelectedItems.Item(1))
For I = 1 To xRng.Areas.Item(1).Cells.Count
With xDoc.Application.Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = xRng.Areas.Item(1).Cells.Item(I).Value
.Replacement.Text = xRng.Areas.Item(2).Cells.Item(I).Value
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
xDoc.Application.Selection.Find.Execute Replace:=wdReplaceAll
Next
ExitSub:
Set xRng = Nothing
Set xFileDlg = Nothing
Set xWordApp = Nothing
Set xDoc = Nothing
End Sub
3. หลังจากวางโค้ดเสร็จแล้ว ขณะที่ยังอยู่ในหน้าต่าง Microsoft Visual Basic for Applications ให้คลิกที่ เครื่องมือ > การอ้างอิง ดูภาพหน้าจอประกอบ:
4. เมื่อกล่องโต้ตอบ References – VBAProject ปรากฏขึ้น ให้เลือก Microsoft Word 16.0 Object Library จากกล่องรายการ ดูภาพหน้าจอประกอบ:
โค้ด VBA: ค้นหาและแทนที่ข้อความหลายรายการในไฟล์ Word หลายไฟล์
Sub FindReplaceAcrossMultipleWordDocuments()
'Updateby ExtendOffice
Dim xWordApp As Word.Application
Dim xDoc As Word.Document
Dim xRng As Range
Dim I As Integer
Dim xFolderDlg As FileDialog
Dim xFSO As Scripting.FileSystemObject
Dim xFile As File
On Error GoTo ExitSub
Set xFolderDlg = Application.FileDialog(msoFileDialogFolderPicker)
If xFolderDlg.Show <> -1 Then GoTo ExitSub
Set xRng = Application.InputBox("Please select the lists of find and replace texts (Press Ctrl key to select two same size ranges", "Kutools for Excel", , , , , , 8)
If xRng.Areas.Count <> 2 Then
MsgBox "Please select two columns (press Ctrl key), the two ranges have the same size", vbInformation + vbOKOnly, "Kutools for Excel"
GoTo ExitSub
End If
If (xRng.Areas.Item(1).Rows.Count <> xRng.Areas.Item(2).Rows.Count) Or _
(xRng.Areas.Item(1).Columns.Count <> xRng.Areas.Item(2).Columns.Count) Then
MsgBox "Please select two columns (press Ctrl key), the two ranges have the same size.", vbInformation + vbOKOnly, "Kutools for Excel"
GoTo ExitSub
End If
Set xFSO = New Scripting.FileSystemObject
Set xWordApp = CreateObject("Word.application")
xWordApp.Visible = True
For Each xFile In xFSO.GetFolder(xFolderDlg.SelectedItems(1)).Files
If VBA.InStr(xFile.Type, "Microsoft Word") > 0 Then
Set xDoc = xWordApp.Documents.Open(xFile.Path)
For I = 1 To xRng.Areas.Item(1).Cells.Count
With xDoc.Application.Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = xRng.Areas.Item(1).Cells.Item(I).Value
.Replacement.Text = xRng.Areas.Item(2).Cells.Item(I).Value
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
xDoc.Application.Selection.Find.Execute Replace:=wdReplaceAll
Next
xDoc.Close wdSaveChanges
End If
Next
xWordApp.Quit
MsgBox "The Find and Replace has been completed", vbInformation + vbOKOnly, "Kutools for Excel"
ExitSub:
Set xRng = Nothing
Set xFolderDlg = Nothing
Set xWordApp = Nothing
Set xDoc = Nothing
End Sub
3. ในขณะที่อยู่ในหน้าต่าง Microsoft Visual Basic for Applications ให้คลิกที่ เครื่องมือ > การอ้างอิง จากนั้นในกล่องโต้ตอบ References – VBAProject ให้เลือก Microsoft Word 16.0 Object Library และ Microsoft Scripting Runtime จากรายการ ดังภาพหน้าจอประกอบ:
ค้นหาและแทนที่ข้อความหลายรายการในเอกสาร Word หลายฉบับด้วยคุณสมบัติอันทรงพลัง
ในส่วนนี้ ฉันจะอธิบายถึงวิธีวิธีค้นหาและแทนที่ข้อความจำนวนมากในเอกสาร Word หลายฉบับโดยใช้ Word เอง แทนที่จะใช้ Excel ด้วยการใช้ Kutools for Word อันทรงพลัง คุณสามารถค้นหาข้อความเฉพาะและแทนที่ด้วยข้อความใหม่ในส่วนเนื้อหาหลัก ส่วนหัว ส่วนท้าย ความคิดเห็น และอื่นๆ ได้อย่างมีประสิทธิภาพ พร้อมทั้งเน้นผลลัพธ์ตามที่ต้องการ
Kutools for Word
Kutools for Word ช่วยให้คุณจัดการงานเอกสารในชีวิตประจำวันได้รวดเร็วขึ้นด้วยเครื่องมือที่ใช้งานได้จริงซึ่งติดตั้งมาใน Microsoft Word โดยไม่ต้องเขียนโค้ดหรือตั้งค่าที่ซับซ้อน
การเขียน การขัดเกลาภาษา การแปล และการสรุปความด้วย AI
ทำงานได้มากขึ้นในเวลาที่น้อยลงด้วย Kutools for Word ที่เสริมพลังด้วย AI
Kutools for Word ไม่ได้เป็นเพียงชุดเครื่องมือเท่านั้น แต่เป็นโซลูชันอัจฉริยะที่ออกแบบมาเพื่อเพิ่มประสิทธิภาพการทำงานของคุณ ด้วยความสามารถที่ขับเคลื่อนด้วย AI และคุณสมบัติที่สำคัญที่สุด Kutools ช่วยให้คุณทำงานได้สำเร็จมากขึ้นในเวลาที่น้อยลง: