メインコンテンツへスキップ

Excel から Word 文書内のテキストを検索して置換する方法

Word 文書では、検索と置換機能を適用して、XNUMX つのテキストをすばやく検索して置換できます。 ただし、複数のテキストを検索して置換する必要がある場合、検索と置換機能にテキストを XNUMX つずつ入力すると時間がかかります。 この場合、検索と置換のテキストをセルのリストに入力し、Excel の VBA コードを使用してこの作業を簡単に行うことができます。 この記事では、複数の Word 文書内のテキストを一括検索して置換する便利な機能も紹介します。

Excel から XNUMX つの Word 文書内の複数のテキストを検索して VBA コードに置き換える

Excel から複数の Word 文書内の複数のテキストを検索して VBA コードに置き換える

複数の Word ドキュメント内の複数のテキストを検索して置換する強力な機能


Excel から XNUMX つの Word 文書内の複数のテキストを検索して VBA コードに置き換える

XNUMX つの Word ファイルだけでいくつかのテキストを検索して置換したい場合は、次の VBA コードが役に立ちます。

1. Excel ワークシートで、検索して置換するテキストを含む列と、下のスクリーンショットに示すように置換するテキストを含む別の列を作成します。 そして、押します Alt + F11 キーを同時に開いて アプリケーション向け Microsoft Visual Basic 窓。

2。 次に、をクリックします。 インセット > モジュールで、以下の VBA コードをコピーしてウィンドウに貼り付けます。

VBA コード: XNUMX つの 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 Word16.0オブジェクトライブラリ リストボックスから、スクリーンショットを参照してください:

5に設定します。 OK をクリックします。 OK ボタンをクリックしてダイアログ ボックスを閉じ、次に を押します。 F5 キーを押してこのコードを実行し、ポップアップ ブラウズ ウィンドウで、テキストを置き換える Word ファイルを選択します。スクリーンショットを参照してください。

6。 次に、をクリックします。 OK、次のダイアログ ボックスで、 Ctrlキー キーを使用して、使用する元のテキストと新しいテキスト セルを個別に選択します。スクリーンショットを参照してください。

7。 次に、をクリックします OK ボタンをクリックすると、テキストが検出され、指定したドキュメントの新しいテキストに置き換えられ、ファイルも開かれます。変更を保持するには、ファイルを保存する必要があります。


Excel から複数の Word 文書内の複数のテキストを検索して VBA コードに置き換える

ここで、複数の Word ドキュメント内の複数のテキストを検索して置換するための VBA コードも作成します。次のようにしてください。

1. 下のスクリーンショットに示すように、置換および置換する値の XNUMX つの列を含む 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 Word16.0オブジェクトライブラリ & Microsoftスクリプトランタイム リストボックスからのオプション、スクリーンショットを参照してください:

4. XNUMX つのオプションを確認したら、 OK ダイアログボックスを閉じてから、 F5 オープニングで、このコードを実行するためのキー ブラウズ ウィンドウで、検索と置換を実行する Word ドキュメントを含むフォルダーを選択します。スクリーンショットを参照してください。

5に設定します。 OK をクリックします。 OK ボタンをクリックし、ポップアウトされたダイアログ ボックスで、 Ctrlキー キーを使用して、使用する元のテキストと新しいテキスト列を個別に選択します。スクリーンショットを参照してください。

6。 最後に、 OK、およびこれらのファイル全体で元のテキストが新しいテキストに置き換えられます。完了すると、以下のスクリーンショットのようにダイアログボックスが表示されます。

7に設定します。 OK をクリックします。 OK ダイアログを閉じます。 ファイルに移動して、変換された結果を確認できます。


複数の Word ドキュメント内の複数のテキストを検索して置換する強力な機能

このセクションでは、Excel ではなく Word から複数の Word 文書のテキストをバッチ検索して置換する方法について説明します。 強力なツールでKutools for Word、特定のテキストをすばやく見つけて置き換え、メインファイル、ヘッダー、フッター、コメントなどの新しいテキストに置き換え、必要に応じて結果を強調表示できます.

1. XNUMX つの Word ファイルを開き、[ クツールズプラス > バッチ検索と置換、スクリーンショットを参照してください:

2。 オープンで バッチ検索と置換 ダイアログボックスで、次の操作を行ってください。

  • Add ボタンをクリックして、テキストを検索して置換する Word ファイルを追加します。
  • 左側のペインで、をクリックします。 行を追加する 一番上のリボンから;
  • 挿入されたフィールドで、元のテキストと新しいテキストを もう完成させ、ワークスペースに掲示しましたか? & 交換する 検索して置換する列を個別に指定します。 同様に、必要に応じて、置換されたテキストを強調表示する色を指定できます。

3. 検索条件を作成したら、 交換する に行くボタン プレビュー結果 タブをクリックして、検索と置換の結果を表示します。 スクリーンショットを参照してください:

4。 次に、をクリックします。 閉じる ボタンをクリックすると、このシナリオを保存するかどうかを確認するプロンプト ボックスが表示されます。 有り 保存するには、をクリックします いいえ 無視するには、スクリーンショットを参照してください。

ヒント: この機能は、次の操作の実現にも役立ちます。
  • 複数の Word 文書で特殊文字を検索して置換します。
  • 複数の Word ドキュメントで特定の書式設定を持つ複数の文字列を検索して置換します。
  • 複数の txt/htm/html ファイルで複数の文字列を検索して置換します。

クリックして、この機能の詳細情報を確認してください…

最高のオフィス生産性向上ツール

🤖 Kutools AI アシスタント: 以下に基づいてデータ分析に革命をもたらします。 インテリジェントな実行   |  コードを生成  |  カスタム数式の作成  |  データを分析してグラフを生成する  |  Kutools関数を呼び出す...
人気の機能: 重複を検索、強調表示、または識別する   |  空白行を削除する   |  データを失わずに列またはセルを結合する   |   数式なしのラウンド ...
スーパールックアップ: 複数の基準の VLookup    複数の値の VLookup  |   複数のシートにわたる VLookup   |   ファジールックアップ ....
詳細ドロップダウン リスト: ドロップダウンリストを素早く作成する   |  依存関係のドロップダウン リスト   |  複数選択のドロップダウンリスト ....
列マネージャー: 特定の数の列を追加する  |  列の移動  |  Toggle 非表示列の表示ステータス  |  範囲と列の比較 ...
注目の機能: グリッドフォーカス   |  デザインビュー   |   ビッグフォーミュラバー    ワークブックとシートマネージャー   |  リソースライブラリ (自動テキスト)   |  日付ピッカー   |  ワークシートを組み合わせる   |  セルの暗号化/復号化    リストごとにメールを送信する   |  スーパーフィルター   |   特殊フィルター (太字/斜体/取り消し線をフィルター...) ...
上位 15 のツールセット12 テキスト ツール (テキストを追加, 文字を削除する、...)   |   50+ チャート 種類 (ガントチャート、...)   |   40+ 実用的 (誕生日に基づいて年齢を計算する、...)   |   19 挿入 ツール (QRコードを挿入, パスから画像を挿入、...)   |   12 変換 ツール (数字から言葉へ, 通貨の換算、...)   |   7 マージ&スプリット ツール (高度な結合行, 分割セル、...)   |   ... もっと

Kutools for Excel で Excel スキルを強化し、これまでにない効率を体験してください。 Kutools for Excelは、生産性を向上させ、時間を節約するための300以上の高度な機能を提供します。  最も必要な機能を入手するにはここをクリックしてください...

説明


Officeタブは、タブ付きのインターフェイスをOfficeにもたらし、作​​業をはるかに簡単にします

  • Word、Excel、PowerPointでタブ付きの編集と読み取りを有効にする、パブリッシャー、アクセス、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