DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: Multiple Find and replace in a word file  (Read 5,779 times)

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,947
Multiple Find and replace in a word file
« on: January 26, 2021, 04:07 PM »
Microsoft word have a find and replace option to search for a string and replace with another one.

But I need to search for several strings and replace each of them by a replacement one. So i need a multiple find and replace .

Do you know an utility to do this ?

Best Regards

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,947
Re: Multiple Find and replace in a word file
« Reply #1 on: January 26, 2021, 04:29 PM »
4wd  ;D

this seems go well for me :

https://www.datanumen.com/blogs/find-replace-multiple-items-word-document/

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,947
Re: Multiple Find and replace in a word file
« Reply #2 on: January 27, 2021, 04:27 PM »
Can I change the delimiters ?
The script tells to seperate the strings with "," .

Can I use other delimiter ? Sometimess the string itself contain a comma....
:-[

Note : I think I can try to modify the delimiters by try and result.

« Last Edit: January 27, 2021, 06:47 PM by Contro »

4wd

  • Pedantic Old Bastard
  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,652
    • Coding Snacks
Re: Multiple Find and replace in a word file
« Reply #3 on: January 27, 2021, 11:33 PM »
Code: ( vb)
Sub FindAndReplaceMultiItems()
  Dim strFindText As String
  Dim strReplaceText As String
  Dim nSplitItem As Long
   
  Application.ScreenUpdating = False

  ' Enter items to be replaces and new ones.
  strFindText = InputBox("Enter items to be found here,seperated by | (vertical bar): ", "Items to be found")
  strReplaceText = InputBox("Enter new items here, seperated by | (vertical bar): ", "New items")
  nSplitItem = UBound(Split(strFindText, "|"))

  ' Find each item and replace it with new one respectively.
  For nSplitItem = 0 To nSplitItem
    With Selection
      .HomeKey Unit:=wdStory
      With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = Split(strFindText, "|")(nSplitItem)
        .Replacement.Text = Split(strReplaceText, "|")(nSplitItem)
        .Format = False
        .MatchWholeWord = False
      End With
    Selection.Find.Execute Replace:=wdReplaceAll
  End With
Next nSplitItem

  Application.ScreenUpdating = True

End Sub

Try that, it uses | instead. (Not tested since I'm on a tablet.)
"I will not follow where the path may lead, but I will go where there is no path, and I will leave a trail." - Muriel Strode, 1903

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,947
Re: Multiple Find and replace in a word file
« Reply #4 on: January 28, 2021, 11:45 AM »
Code: ( vb)
Sub FindAndReplaceMultiItems()
  Dim strFindText As String
  Dim strReplaceText As String
  Dim nSplitItem As Long
   
  Application.ScreenUpdating = False

  ' Enter items to be replaces and new ones.
  strFindText = InputBox("Enter items to be found here,seperated by | (vertical bar): ", "Items to be found")
  strReplaceText = InputBox("Enter new items here, seperated by | (vertical bar): ", "New items")
  nSplitItem = UBound(Split(strFindText, "|"))

  ' Find each item and replace it with new one respectively.
  For nSplitItem = 0 To nSplitItem
    With Selection
      .HomeKey Unit:=wdStory
      With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = Split(strFindText, "|")(nSplitItem)
        .Replacement.Text = Split(strReplaceText, "|")(nSplitItem)
        .Format = False
        .MatchWholeWord = False
      End With
    Selection.Find.Execute Replace:=wdReplaceAll
  End With
Next nSplitItem

  Application.ScreenUpdating = True

End Sub

Try that, it uses | instead. (Not tested since I'm on a tablet.)
4wd i will try and comment

Best Regards
:-* :P

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,947
Re: Multiple Find and replace in a word file
« Reply #5 on: January 29, 2021, 05:23 PM »
Works fine !!!!!!

Original address: https://www.donationcoder.com/forum/index.php?topic=51006.0