Loncat ke daftar isi utama

Bagaimana menemukan dan mengganti teks dalam dokumen Word dari Excel?

Dalam dokumen Word, kita dapat menerapkan fungsi Find and Replace untuk menemukan dan mengganti satu teks dengan cepat. Tapi, jika ada beberapa teks yang perlu ditemukan dan diganti, memasukkan teks satu per satu ke fitur Find and Replace akan memakan waktu. Dalam hal ini, Anda dapat memasukkan temukan dan ganti teks ke dalam daftar sel, dan dengan bantuan kode VBA di Excel untuk menyelesaikan pekerjaan ini dengan mudah. Pada artikel ini, saya juga akan memperkenalkan fitur yang berguna untuk menemukan dan mengganti teks dalam beberapa dokumen Word.

Temukan dan ganti beberapa teks dalam satu dokumen Word dari Excel dengan kode VBA

Temukan dan ganti banyak teks dalam beberapa dokumen Word dari Excel dengan kode VBA

Temukan dan ganti banyak teks dalam beberapa dokumen Word dengan fitur canggih


Temukan dan ganti beberapa teks dalam satu dokumen Word dari Excel dengan kode VBA

Jika Anda ingin menemukan dan mengganti beberapa teks hanya dalam satu file Word, kode VBA berikut dapat membantu Anda.

1. Di lembar kerja Excel, buat kolom yang berisi teks yang ingin Anda temukan dan ganti, dan kolom lain dengan teks untuk diganti seperti gambar di bawah ini. Dan kemudian tekan Alt + F11 tombol secara bersamaan untuk membuka Microsoft Visual Basic untuk Aplikasi jendela.

2. Lalu klik Menyisipkan > Modul, salin dan tempel kode VBA di bawah ini ke jendela.

Kode VBA: Temukan dan ganti beberapa teks dalam satu file 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. Setelah menempelkan kode, masih dalam Microsoft Visual Basic untuk Aplikasi window, klik Tools > Referensi, lihat tangkapan layar:

4. Di pop-out Referensi - VBAProject kotak dialog, pilih Perpustakaan objek Microsoft Word 16.0 dari kotak daftar, lihat tangkapan layar:

5. Klik OK untuk menutup kotak dialog, dan sekarang, tekan F5 kunci untuk menjalankan kode ini, di jendela Browse pop up, pilih File Word yang ingin Anda ganti teksnya, lihat tangkapan layar:

6. Lalu klik OK, pada kotak dialog berikut, tekan Ctrl untuk memilih teks asli dan sel teks baru secara terpisah yang ingin Anda gunakan, lihat tangkapan layar:

7. Dan kemudian, klik OK tombol, sekarang, teks ditemukan dan diganti dengan teks baru di dokumen yang Anda tentukan, dan file juga terbuka, Anda harus menyimpannya untuk menyimpan perubahan.


Temukan dan ganti banyak teks dalam beberapa dokumen Word dari Excel dengan kode VBA

Di sini, saya juga membuat kode VBA untuk menemukan dan mengganti banyak teks di beberapa dokumen Word, lakukan seperti ini:

1. Buka file Excel yang berisi dua kolom nilai untuk diganti dan diganti seperti gambar di bawah ini, lalu tekan Alt + F11 tombol secara bersamaan untuk membuka Microsoft Visual Basic untuk Aplikasi jendela.

2. Lalu klik Menyisipkan > Modul, salin dan tempel kode VBA di bawah ini ke jendela.

Kode VBA: Temukan dan ganti banyak teks dalam beberapa file 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. Masih di Microsoft Visual Basic untuk Aplikasi window, klik Tools > Referensi, Dalam Referensi - VBAProject kotak dialog, pilih Perpustakaan objek Microsoft Word 16.0 dan Runtime Microsoft Scripting opsi dari kotak daftar, lihat tangkapan layar:

4. Setelah memeriksa dua opsi, dan klik OK untuk menutup kotak dialog, dan kemudian, terus tekan tombol F5 kunci untuk mengeksekusi kode ini, di pembukaan Browse jendela, pilih folder berisi dokumen Word yang ingin Anda cari dan ganti, lihat tangkapan layar:

5. Klik OK tombol, di kotak dialog yang muncul, tekan Ctrl untuk memilih teks asli dan kolom teks baru secara terpisah yang ingin Anda gunakan, lihat tangkapan layar:

6. Akhirnya, klik OK, dan teks asli diganti dengan yang baru di seluruh file tesis, setelah selesai, kotak dialog akan muncul seperti gambar di bawah ini:

7. Klik OK untuk menutup dialog. Dan Anda dapat membuka file untuk memeriksa hasil yang dikonversi.


Temukan dan ganti banyak teks dalam beberapa dokumen Word dengan fitur canggih

Bagian ini, saya akan berbicara tentang cara mengumpulkan dan mengganti teks dalam beberapa dokumen Word dari Word alih-alih Excel. Dengan alat yang ampuh-Kutools for Word, Anda dapat dengan cepat menemukan dan mengganti teks tertentu dan menggantinya dengan teks baru di file utama, header, footer, komentar, dll. dan menyorot hasilnya sesuai kebutuhan.

1. Buka satu file Word, lalu klik Kutools Ditambah > Temukan dan Ganti Batch, lihat tangkapan layar:

2. Di tempat terbuka Temukan dan Ganti Batch kotak dialog, lakukan operasi berikut:

  • Klik Add tombol untuk menambahkan file Word di mana Anda ingin mencari dan mengganti teks;
  • Di panel kiri, klik Menambahkan baris dari pita atas;
  • Di bidang yang disisipkan, masukkan teks asli dan teks baru ke dalam Menemukan dan menggantikan kolom secara terpisah yang ingin Anda temukan dan ganti. Selain itu, Anda dapat menentukan warna untuk menyorot teks yang diganti sesuai kebutuhan.

3. Setelah membuat kriteria pencarian, klik menggantikan tombol untuk pergi ke Pratinjau Hasil tab untuk melihat hasil pencarian dan penggantian. Lihat tangkapan layar:

4. Lalu klik Penyelesaian tombol, dan kotak prompt muncul untuk mengingatkan Anda jika Anda ingin menyimpan skenario ini, klik Yes untuk menyimpannya, dan klik Tidak untuk mengabaikannya, lihat tangkapan layar:

Tips: Fitur ini juga dapat membantu untuk mencapai operasi berikut:
  • Temukan dan ganti karakter khusus di beberapa dokumen Word;
  • Temukan dan ganti beberapa string dengan pemformatan khusus di beberapa dokumen Word;
  • Temukan dan ganti beberapa string dalam beberapa file txt/htm/html.

Klik untuk mengetahui informasi lebih detail tentang fitur ini…

Alat Produktivitas Kantor Terbaik

🤖 Kutools AI Ajudan: Merevolusi analisis data berdasarkan: Eksekusi Cerdas   |  Hasilkan Kode  |  Buat Rumus Khusus  |  Analisis Data dan Hasilkan Grafik  |  Aktifkan Fungsi Kutools...
Fitur Populer: Temukan, Sorot, atau Identifikasi Duplikat   |  Hapus Baris Kosong   |  Gabungkan Kolom atau Sel tanpa Kehilangan Data   |   Putaran tanpa Formula ...
Pencarian Super: VLookup Beberapa Kriteria    VLookup Nilai Berganda  |   VLookup di Beberapa Lembar   |   Pencarian Fuzzy ....
Daftar Drop-down Lanjutan: Buat Daftar Drop Down dengan Cepat   |  Daftar Drop Down yang Bergantung   |  Multi-pilih Drop Down List ....
Manajer Kolom: Tambahkan Jumlah Kolom Tertentu  |  Pindahkan Kolom  |  Alihkan Status Visibilitas Kolom Tersembunyi  |  Bandingkan Rentang & Kolom ...
Fitur Unggulan: Fokus Kisi   |  Tampilan Desain   |   Bar Formula Besar    Manajer Buku Kerja & Lembar   |  Perpustakaan Sumberdaya (Teks otomatis)   |  Pemetik tanggal   |  Gabungkan Lembar Kerja   |  Enkripsi/Dekripsi Sel    Kirim Email berdasarkan Daftar   |  Filter Super   |   Filter Khusus (filter tebal/miring/coret...) ...
15 Perangkat Teratas12 Teks Tools (Tambahkan Teks, Hapus Karakter, ...)   |   50 + Grafik jenis (Gantt Chart, ...)   |   40+ Praktis Rumus (Hitung usia berdasarkan ulang tahun, ...)   |   19 Insersi Tools (Masukkan Kode QR, Sisipkan Gambar dari Jalur, ...)   |   12 Konversi Tools (Angka ke Kata, Konversi Mata Uang, ...)   |   7 Gabungkan & Pisahkan Tools (Lanjutan Gabungkan Baris, Pisahkan Sel, ...)   |   ... dan banyak lagi

Tingkatkan Keterampilan Excel Anda dengan Kutools for Excel, dan Rasakan Efisiensi yang Belum Pernah Ada Sebelumnya. Kutools for Excel Menawarkan Lebih dari 300 Fitur Lanjutan untuk Meningkatkan Produktivitas dan Menghemat Waktu.  Klik Di Sini untuk Mendapatkan Fitur yang Paling Anda Butuhkan...

Deskripsi Produk


Tab Office Membawa antarmuka Tab ke Office, dan Membuat Pekerjaan Anda Jauh Lebih Mudah

  • Aktifkan pengeditan dan pembacaan tab di Word, Excel, PowerPoint, Publisher, Access, Visio, dan Project.
  • Buka dan buat banyak dokumen di tab baru di jendela yang sama, bukan di jendela baru.
  • Meningkatkan produktivitas Anda sebesar 50%, dan mengurangi ratusan klik mouse untuk Anda setiap hari!
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