ไปยังเนื้อหาหลัก

วิธีการค้นหาและแทนที่ข้อความในเอกสาร Word จาก Excel?

ในเอกสาร Word เราสามารถใช้ฟังก์ชันค้นหาและแทนที่เพื่อค้นหาและแทนที่ข้อความได้อย่างรวดเร็ว แต่ถ้าจำเป็นต้องค้นหาและแทนที่ข้อความหลายข้อความ ให้ป้อนข้อความทีละข้อความในคุณลักษณะค้นหาและแทนที่จะใช้เวลานาน ในกรณีนี้ คุณสามารถป้อนข้อความค้นหาและแทนที่ลงในรายการเซลล์ และด้วยความช่วยเหลือของโค้ด VBA ใน Excel เพื่อทำงานนี้ให้สำเร็จได้อย่างง่ายดาย ในบทความนี้ ฉันจะแนะนำคุณลักษณะที่มีประโยชน์ในการค้นหาและแทนที่ข้อความในเอกสาร Word หลายชุด

ค้นหาและแทนที่หลายข้อความในเอกสาร Word เดียวจาก Excel ด้วยรหัส VBA

ค้นหาและแทนที่ข้อความหลายรายการในเอกสาร Word หลายฉบับจาก Excel ด้วยรหัส VBA

ค้นหาและแทนที่ข้อความหลายรายการในเอกสาร Word หลายฉบับด้วยคุณสมบัติอันทรงพลัง


ค้นหาและแทนที่หลายข้อความในเอกสาร Word เดียวจาก Excel ด้วยรหัส VBA

หากคุณต้องการค้นหาและแทนที่ข้อความบางข้อความในไฟล์ Word เพียงไฟล์เดียว โค้ด VBA ต่อไปนี้สามารถช่วยคุณได้

1. ในเวิร์กชีต Excel ให้สร้างคอลัมน์ที่มีข้อความที่คุณต้องการค้นหาและแทนที่ และคอลัมน์อื่นที่มีข้อความที่จะแทนที่ด้วยภาพหน้าจอด้านล่างที่แสดง แล้วกด Alt + F11 พร้อมกันเพื่อเปิดไฟล์ Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

2. จากนั้นคลิก สิ่งที่ใส่เข้าไป > โมดูลให้คัดลอกและวางโค้ด VBA ด้านล่างลงในหน้าต่าง

รหัส 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 สำหรับแอปพลิเคชัน หน้าต่างคลิก เครื่องมือ > อ้างอิงดูภาพหน้าจอ:

4. ในการโผล่ออกมา เอกสารอ้างอิง - VBAProject กล่องโต้ตอบเลือกไฟล์ ไลบรารีวัตถุ Microsoft Word 16.0 จากกล่องรายการ ดูภาพหน้าจอ:

5. คลิก OK เพื่อปิดกล่องโต้ตอบ และตอนนี้ ให้กด F5 เพื่อเรียกใช้รหัสนี้ ในหน้าต่างป๊อปอัป เรียกดู ให้เลือก ไฟล์ Word ที่คุณต้องการแทนที่ข้อความ ดูภาพหน้าจอ:

6. จากนั้นคลิก OKในกล่องโต้ตอบต่อไปนี้ ให้กด Ctrl เพื่อเลือกข้อความต้นฉบับและเซลล์ข้อความใหม่แยกกันที่คุณต้องการใช้ ดูภาพหน้าจอ:

7. จากนั้นคลิก OK ปุ่ม ตอนนี้ ข้อความถูกพบและแทนที่ด้วยข้อความใหม่ในเอกสารที่คุณระบุ และไฟล์กำลังเปิดอยู่เช่นกัน คุณควรบันทึกเพื่อเก็บการเปลี่ยนแปลง


ค้นหาและแทนที่ข้อความหลายรายการในเอกสาร Word หลายฉบับจาก Excel ด้วยรหัส VBA

ที่นี่ ฉันยังสร้างโค้ด VBA สำหรับค้นหาและแทนที่ข้อความหลายข้อความในเอกสาร Word หลายฉบับ โปรดทำดังนี้:

1. เปิดไฟล์ Excel ที่มีค่าสองคอลัมน์เพื่อแทนที่และแทนที่ด้วยภาพหน้าจอด้านล่างที่แสดง จากนั้นกด Alt + F11 พร้อมกันเพื่อเปิดไฟล์ Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

2. จากนั้นคลิก สิ่งที่ใส่เข้าไป > โมดูลให้คัดลอกและวางโค้ด VBA ด้านล่างลงในหน้าต่าง

รหัส 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 สำหรับแอปพลิเคชัน หน้าต่างคลิก เครื่องมือ > อ้างอิงใน เอกสารอ้างอิง - VBAProject กล่องโต้ตอบเลือกไฟล์ ไลบรารีวัตถุ Microsoft Word 16.0 และ รันไทม์การเขียนสคริปต์ของ Microsoft ตัวเลือกจากกล่องรายการ ดูภาพหน้าจอ:

4. หลังจากตรวจสอบทั้งสองตัวเลือกแล้ว คลิก OK เพื่อปิดกล่องโต้ตอบ จากนั้นให้กดปุ่ม F5 คีย์เพื่อรันโค้ดนี้ในการเปิด หมวดหมู่สินค้า หน้าต่าง เลือกโฟลเดอร์ที่มีเอกสาร Word ที่คุณต้องการทำการค้นหาและแทนที่ ดูภาพหน้าจอ:

5. คลิก OK ในกล่องโต้ตอบที่ปรากฏขึ้น ให้กด Ctrl เพื่อเลือกข้อความต้นฉบับและคอลัมน์ข้อความใหม่แยกกันที่คุณต้องการใช้ ดูภาพหน้าจอ:

6. สุดท้ายคลิก OKและข้อความต้นฉบับจะถูกแทนที่ด้วยข้อความใหม่ในไฟล์เหล่านี้ หลังจากเสร็จสิ้น กล่องโต้ตอบจะปรากฏขึ้นดังภาพด้านล่างที่แสดง:

7. คลิก OK เพื่อปิดกล่องโต้ตอบ และคุณสามารถไปที่ไฟล์เพื่อตรวจสอบผลลัพธ์ที่แปลงแล้ว


ค้นหาและแทนที่ข้อความหลายรายการในเอกสาร Word หลายฉบับด้วยคุณสมบัติอันทรงพลัง

ส่วนนี้ฉันจะพูดถึงวิธีค้นหาและแทนที่ข้อความในเอกสาร Word หลายฉบับจาก Word แทนที่จะเป็น Excel ด้วยเครื่องมืออันทรงพลัง-Kutools สำหรับ Wordคุณสามารถค้นหาและแทนที่ข้อความที่ต้องการได้อย่างรวดเร็ว และแทนที่ด้วยข้อความใหม่ในไฟล์หลัก ส่วนหัว ส่วนท้าย ความคิดเห็น ฯลฯ และเน้นผลลัพธ์ตามที่คุณต้องการ

1. เปิดไฟล์ Word หนึ่งไฟล์ จากนั้นคลิก Kutools พลัส > ค้นหาแบทช์และแทนที่ดูภาพหน้าจอ:

2. ในการเปิด ค้นหาแบทช์และแทนที่ โปรดดำเนินการดังต่อไปนี้:

  • คลิก เพิ่ม ปุ่มเพื่อเพิ่มไฟล์ Word ที่คุณต้องการค้นหาและแทนที่ข้อความ
  • ในบานหน้าต่างด้านซ้ายคลิก เพิ่มแถว จากริบบิ้นด้านบน
  • ในช่องที่แทรก ให้ป้อนข้อความต้นฉบับและข้อความใหม่ลงในช่อง หา และ แทนที่ แยกคอลัมน์ที่คุณต้องการค้นหาและแทนที่ นอกจากนี้ คุณสามารถระบุสีสำหรับเน้นข้อความที่แทนที่ได้ตามต้องการ

3. หลังจากสร้างเกณฑ์การค้นหาแล้ว ให้คลิก แทนที่ ปุ่มไปที่ ดูตัวอย่างผลลัพธ์ เพื่อดูผลลัพธ์การค้นหาและแทนที่ ดูภาพหน้าจอ:

4. จากนั้นคลิก ปิดหน้านี้ และกล่องข้อความจะปรากฏขึ้นเพื่อเตือนคุณว่าคุณต้องการบันทึกสถานการณ์นี้หรือไม่ ให้คลิก ใช่ เพื่อบันทึกและคลิก ไม่ ไม่สนใจดูภาพหน้าจอ:

เคล็ดลับ: คุณลักษณะนี้ยังสามารถช่วยให้ดำเนินการดังต่อไปนี้ได้สำเร็จ:
  • ค้นหาและแทนที่อักขระพิเศษในเอกสาร Word หลายฉบับ
  • ค้นหาและแทนที่สตริงหลายรายการด้วยการจัดรูปแบบเฉพาะในเอกสาร Word หลายฉบับ
  • ค้นหาและแทนที่หลายสตริงในไฟล์ txt/htm/html หลายไฟล์

คลิกเพื่อทราบข้อมูลรายละเอียดเพิ่มเติมของคุณสมบัตินี้...

สุดยอดเครื่องมือเพิ่มผลผลิตในสำนักงาน

🤖 Kutools AI ผู้ช่วย: ปฏิวัติการวิเคราะห์ข้อมูลโดยยึดตาม: การดำเนินการที่ชาญฉลาด   |  สร้างรหัส  |  สร้างสูตรที่กำหนดเอง  |  วิเคราะห์ข้อมูลและสร้างแผนภูมิ  |  เรียกใช้ฟังก์ชัน Kutools...
คุณสมบัติยอดนิยม: ค้นหา เน้น หรือระบุรายการที่ซ้ำกัน   |  ลบแถวว่าง   |  รวมคอลัมน์หรือเซลล์โดยไม่สูญเสียข้อมูล   |   รอบโดยไม่มีสูตร ...
การค้นหาขั้นสูง: VLookup หลายเกณฑ์    VLookup หลายค่า  |   VLookup ข้ามหลายแผ่น   |   การค้นหาที่ไม่ชัดเจน ....
รายการแบบเลื่อนลงขั้นสูง: สร้างรายการแบบหล่นลงอย่างรวดเร็ว   |  รายการแบบหล่นลงขึ้นอยู่กับ   |  เลือกหลายรายการแบบหล่นลง ....
ผู้จัดการคอลัมน์: เพิ่มจำนวนคอลัมน์เฉพาะ  |  ย้ายคอลัมน์  |  สลับสถานะการมองเห็นของคอลัมน์ที่ซ่อนอยู่  |  เปรียบเทียบช่วงและคอลัมน์ ...
คุณสมบัติเด่น: กริดโฟกัส   |  มุมมองการออกแบบ   |   บาร์สูตรใหญ่    สมุดงานและตัวจัดการชีต   |  ห้องสมุดทรัพยากร (ข้อความอัตโนมัติ)   |  เลือกวันที่   |  รวมแผ่นงาน   |  เข้ารหัส/ถอดรหัสเซลล์    ส่งอีเมลตามรายการ   |  ซุปเปอร์ฟิลเตอร์   |   ตัวกรองพิเศษ (กรองตัวหนา/ตัวเอียง/ขีดทับ...) ...
ชุดเครื่องมือ 15 อันดับแรก12 ข้อความ เครื่องมือ (เพิ่มข้อความ, ลบอักขระ, ... )   |   50 + แผนภูมิ ประเภท (แผนภูมิ Gantt, ... )   |   40+ ใช้งานได้จริง สูตร (คำนวณอายุตามวันเกิด, ... )   |   19 การแทรก เครื่องมือ (ใส่ QR Code, แทรกรูปภาพจากเส้นทาง, ... )   |   12 การแปลง เครื่องมือ (ตัวเลขเป็นคำ, การแปลงสกุลเงิน, ... )   |   7 ผสานและแยก เครื่องมือ (แถวรวมขั้นสูง, แยกเซลล์, ... )   |   ... และอื่น ๆ

เพิ่มพูนทักษะ Excel ของคุณด้วย Kutools สำหรับ Excel และสัมผัสประสิทธิภาพอย่างที่ไม่เคยมีมาก่อน Kutools สำหรับ Excel เสนอคุณสมบัติขั้นสูงมากกว่า 300 รายการเพื่อเพิ่มประสิทธิภาพและประหยัดเวลา  คลิกที่นี่เพื่อรับคุณสมบัติที่คุณต้องการมากที่สุด...

รายละเอียด


แท็บ Office นำอินเทอร์เฟซแบบแท็บมาที่ Office และทำให้งานของคุณง่ายขึ้นมาก

  • เปิดใช้งานการแก้ไขและอ่านแบบแท็บใน Word, Excel, PowerPoint, ผู้จัดพิมพ์, Access, Visio และโครงการ
  • เปิดและสร้างเอกสารหลายรายการในแท็บใหม่ของหน้าต่างเดียวกันแทนที่จะเป็นในหน้าต่างใหม่
  • เพิ่มประสิทธิภาพการทำงานของคุณ 50% และลดการคลิกเมาส์หลายร้อยครั้งให้คุณทุกวัน!
Comments (10)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
This works great, thank you! Is there a way to make the replacement text carry hyperlinks over? ie - if you have a hyperlinked replacement in the excel sheet, it is still hyperlinked in the Word doc?

Thanks!
This comment was minimized by the moderator on the site
Is there a way too modify this too find text and create hyperlink on the text from another column where i have the links already created? It worked correctly as a find and replace for me. Thanks
This comment was minimized by the moderator on the site
Hi,

I am wondering how this can be modified to also find and replace text in footnotes?

Thanks!
This comment was minimized by the moderator on the site
Hello, Nate,
If you want to find and replace the text in footnotes at the same time, maybe the Kutools for Word's Batch Find and Replace feature can help you.
You just need to check Main document and Footnotes from the Find in section, see below image:
https://www.extendoffice.com/images/stories/comments/comment-skyyang/2023-comment/doc-find-replace-word.png
This comment was minimized by the moderator on the site
It doesn't work.

Compile error: User-defined type not defined
This comment was minimized by the moderator on the site
Hello, Param
The code works well.
Maybe, you didn't check Microsoft Word 16.0 Object Library from the References – VBAProject dialog box.
It means that you may miss the Step 3 and Step 4 of this article.
Please try again, if you still have any other problem, please comment here.
https://www.extendoffice.com/images/stories/comments/comment-skyyang/2023-comment/doc-find-replace-word-file-excel.png
This comment was minimized by the moderator on the site
Sorry for the overdue reply. I have replied before, but my reply dissapeared somehow. You're right, the code does work well. But it replaced nothing when I tried it on a file with more than 80,000 lines.
This comment was minimized by the moderator on the site
Hello, Param
I have tested the code, it works well in my Word docuent which contains 140,000 lines.
Do you mind to upload your attachment here for testing?
Or you can apply our Kutools for Word's Batch Find and Replace feature, it can help you with ease.
Thank you!
This comment was minimized by the moderator on the site
Greetings,
the first code :
VBA code: Find and replace multiple texts in one Word file

thows error : compile error user defined type not defined
https://i.imgur.com/FZPBy4I.png
This comment was minimized by the moderator on the site
Hello, Erik
The code works well.
Maybe, you didn't check Microsoft Word 16.0 Object Library from the References – VBAProject dialog box.
It means that you may miss the Step 3 and Step 4 of this article.
Please try again, if you still have any other problem, please comment here.

https://www.extendoffice.com/images/stories/comments/comment-skyyang/2023-comment/doc-find-replace-word-file-excel.png
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations