انتقل إلى المحتوى الرئيسي

كيفية البحث عن نص واستبداله في مستند Word من Excel؟

في مستند Word ، يمكننا تطبيق وظيفة البحث والاستبدال للعثور على نص واحد واستبداله بسرعة. ولكن ، إذا كانت هناك عدة نصوص تحتاج إلى العثور عليها واستبدالها ، فأدخل النص واحدًا تلو الآخر في ميزة البحث والاستبدال سيستغرق وقتًا طويلاً. في هذه الحالة ، يمكنك إدخال البحث عن النصوص واستبدالها في قائمة الخلايا ، وبمساعدة رمز VBA في Excel لإنجاز هذه المهمة بسهولة. في هذه المقالة ، سأقدم أيضًا ميزة مفيدة للبحث عن النصوص واستبدالها في مستندات Word متعددة.

ابحث عن نصوص متعددة واستبدلها في مستند Word واحد من Excel برمز VBA

ابحث عن نصوص متعددة واستبدلها في مستندات Word متعددة من Excel برمز VBA

ابحث عن نصوص متعددة واستبدلها في مستندات Word متعددة بميزة قوية


ابحث عن نصوص متعددة واستبدلها في مستند Word واحد من Excel برمز VBA

إذا كنت ترغب في البحث عن بعض النصوص واستبدالها في ملف Word واحد فقط ، فإن رمز VBA التالي يمكن أن يقدم لك خدمة.

1. في ورقة عمل Excel ، قم بإنشاء عمود يحتوي على النصوص التي تريد البحث عنها واستبدالها ، وعمودًا آخر يحتوي على نصوص لاستبدالها كما هو موضح أدناه. ثم اضغط على Alt + F11 مفاتيح في نفس الوقت لفتح ميكروسوفت فيسوال باسيك للتطبيقات نافذة.

2. ثم اضغط إدراج > وحدة، انسخ والصق رمز فبا أدناه في النافذة.

رمز 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. بعد لصق الكود ، لا يزال في ملف ميكروسوفت فيسوال باسيك للتطبيقات الإطار، انقر فوق الأدوات > مراجع حسابات، انظر لقطة الشاشة:

4. في المنبثقة المراجع - VBAProject في مربع الحوار ، حدد ملف مكتبة كائنات Microsoft Word 16.0 من مربع القائمة ، انظر لقطة الشاشة:

5. انقر OK زر لإغلاق مربع الحوار ، والآن ، اضغط على F5 مفتاح لتشغيل هذا الرمز ، في نافذة التصفح المنبثقة ، حدد ملف Word الذي تريد استبدال النصوص ، انظر لقطة الشاشة:

6. ثم اضغط OK، في مربع الحوار التالي ، اضغط على CTRL مفتاح لتحديد النص الأصلي وخلايا النص الجديدة التي تريد استخدامها بشكل منفصل ، انظر لقطة الشاشة:

7. ثم انقر فوق OK الآن ، تم العثور على النصوص واستبدالها بالنصوص الجديدة في المستند المحدد الخاص بك ، ويتم فتح الملف أيضًا ، يجب عليك حفظه للاحتفاظ بالتغييرات.


ابحث عن نصوص متعددة واستبدلها في مستندات Word متعددة من Excel برمز VBA

هنا ، أقوم أيضًا بإنشاء رمز VBA للبحث عن نصوص متعددة واستبدالها في مستندات Word متعددة ، يرجى القيام بذلك على النحو التالي:

1. افتح ملف Excel الذي يحتوي على عمودين من القيم لاستبدالهما واستبدالهما كما هو موضح أدناه ، ثم اضغط على Alt + F11 مفاتيح في نفس الوقت لفتح ميكروسوفت فيسوال باسيك للتطبيقات نافذة.

2. ثم اضغط إدراج > وحدة، انسخ والصق رمز فبا أدناه في النافذة.

رمز 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. لا يزال في ميكروسوفت فيسوال باسيك للتطبيقات الإطار، انقر فوق الأدوات > مراجع حسابات، في المراجع - VBAProject في مربع الحوار ، حدد ملف مكتبة كائنات Microsoft Word 16.0 و وقت تشغيل البرمجة لـ Microsoft خيارات من مربع القائمة ، انظر لقطة الشاشة:

4. بعد التحقق من الخيارين ، وانقر فوق OK لإغلاق مربع الحوار ، ثم تابع الضغط على F5 مفتاح لتنفيذ هذا الرمز ، في الافتتاح تصفح نافذة ، اختر مجلدًا يحتوي على مستندات Word التي تريد إجراء البحث والاستبدال ، انظر لقطة الشاشة:

5. انقر OK في مربع الحوار المنبثق ، اضغط على CTRL مفتاح لتحديد النص الأصلي وأعمدة النص الجديدة بشكل منفصل التي تريد استخدامها ، انظر لقطة الشاشة:

6. وأخيرا، انقر فوق OK، ويتم استبدال النصوص الأصلية بالنصوص الجديدة عبر هذه الملفات ، بعد الانتهاء ، سيظهر مربع حوار كما هو موضح أدناه في لقطة الشاشة:

7. انقر OK لإغلاق الحوار. ويمكنك الذهاب إلى الملفات للتحقق من النتائج المحولة.


ابحث عن نصوص متعددة واستبدلها في مستندات Word متعددة بميزة قوية

في هذا القسم ، سأتحدث عن كيفية البحث عن النصوص واستبدالها في مستندات Word متعددة من Word بدلاً من Excel. بأداة قوية-كوتولس للكلمة، يمكنك العثور بسرعة على نصوص محددة واستبدالها بنصوص جديدة في الملف الرئيسي ، والعنوان ، والتذييل ، والتعليقات ، وما إلى ذلك ، وتسليط الضوء على النتائج حسب حاجتك.

1. افتح ملف Word واحدًا ، ثم انقر فوق "نعم" كوتولس بلس > بحث واستبدال دفعة، انظر لقطة الشاشة:

2. في فتح بحث واستبدال دفعة مربع الحوار ، يرجى القيام بالعمليات التالية:

  • انقر أضف زر لإضافة ملفات Word حيث تريد البحث عن النصوص واستبدالها ؛
  • في الجزء الأيمن ، انقر فوق "نعم" اضف سطر من الشريط العلوي
  • في الحقل المدرج ، أدخل النص الأصلي والنص الجديد في ملف و استبدل بشكل منفصل عن الأعمدة التي تريد البحث عنها واستبدالها. كذلك ، يمكنك تحديد لون لإبراز النصوص المستبدلة حسب حاجتك.

3. بعد إنشاء معايير البحث ، انقر فوق استبدل زر للذهاب إلى نتيجة المعاينة علامة التبويب لعرض نتائج البحث والاستبدال. انظر لقطة الشاشة:

4. ثم اضغط اغلاق ، ويظهر مربع موجه لتذكيرك إذا كنت تريد حفظ هذا السيناريو ، انقر فوق نعم لحفظه ، وانقر فوق لا لتجاهلها ، انظر لقطة الشاشة:

نصائح: يمكن أن تساعد هذه الميزة أيضًا في تحقيق العمليات التالية:
  • البحث عن الأحرف الخاصة واستبدالها في مستندات Word المتعددة ؛
  • البحث عن سلاسل متعددة واستبدالها بتنسيق محدد في مستندات Word متعددة ؛
  • ابحث عن سلاسل متعددة واستبدلها في ملفات txt / htm / html متعددة.

انقر لمعرفة المزيد من المعلومات التفصيلية عن هذه الميزة ...

أفضل أدوات إنتاجية المكتب

🤖 مساعد Kutools AI: إحداث ثورة في تحليل البيانات على أساس: التنفيذ الذكي   |  إنشاء التعليمات البرمجية  |  إنشاء صيغ مخصصة  |  تحليل البيانات وإنشاء الرسوم البيانية  |  استدعاء وظائف Kutools...
الميزات الشعبية: البحث عن التكرارات أو تمييزها أو تحديدها   |  حذف الصفوف الفارغة   |  دمج الأعمدة أو الخلايا دون فقدان البيانات   |   جولة بدون صيغة 
سوبر بحث: معايير متعددة VLookup    VLookup ذات القيمة المتعددة  |   VLookup عبر أوراق متعددة   |   بحث غامض ....
قائمة منسدلة متقدمة: إنشاء القائمة المنسدلة بسرعة   |  القائمة المنسدلة التابعة   |  قائمة منسدلة متعددة التحديد ....
مدير العمود: إضافة عدد محدد من الأعمدة  |  نقل الأعمدة  |  تبديل حالة رؤية الأعمدة المخفية  |  مقارنة النطاقات والأعمدة 
الميزات المميزة: التركيز على الشبكة   |  عرض تصميم   |   شريط الفورمولا الكبير    مدير المصنفات والأوراق   |  مكتبة الموارد (النص السيارات)   |  منتقي التاريخ   |  اجمع أوراق العمل   |  تشفير/فك تشفير الخلايا    إرسال رسائل البريد الإلكتروني عن طريق القائمة   |  سوبر تصفية   |   مرشح خاص (تصفية غامق / مائل / يتوسطه خط ...) ...
أفضل 15 مجموعة أدوات12 نص الأدوات (إضافة نص, إزالة الأحرف، ...)   |   +50 رسم الأنواع (مخطط جانت، ...)   |   40+ عملي الصيغ (احسب العمر على أساس تاريخ الميلاد، ...)   |   19 إدخال الأدوات (أدخل رمز الاستجابة السريعة, إدراج صورة من المسار، ...)   |   12 تحويل الأدوات (أرقام إلى كلمات, نتيجة تحويل عملة، ...)   |   7 دمج وتقسيم الأدوات (الجمع بين الصفوف المتقدمة, تقسيم الخلايا، ...)   |   ... و اكثر

عزز مهاراتك في Excel باستخدام Kutools for Excel، واختبر كفاءة لم يسبق لها مثيل. يقدم Kutools for Excel أكثر من 300 ميزة متقدمة لتعزيز الإنتاجية وتوفير الوقت.  انقر هنا للحصول على الميزة التي تحتاجها أكثر...

الوصف


يجلب Office Tab الواجهة المبوبة إلى 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