topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday March 28, 2024, 6:54 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Multiple Find and replace in a word file  (Read 2873 times)

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Multiple Find and replace in a word file
« on: January 26, 2021, 10:07 AM »
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,940
    • View Profile
    • Donate to Member
Re: Multiple Find and replace in a word file
« Reply #1 on: January 26, 2021, 10:29 AM »
4wd  ;D

this seems go well for me :

https://www.datanume...items-word-document/

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Multiple Find and replace in a word file
« Reply #2 on: January 27, 2021, 10:27 AM »
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, 12:47 PM by Contro »

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,641
    • View Profile
    • Donate to Member
Re: Multiple Find and replace in a word file
« Reply #3 on: January 27, 2021, 05:33 PM »
Code: Visual Basic [Select]
  1. Sub FindAndReplaceMultiItems()
  2.   Dim strFindText As String
  3.   Dim strReplaceText As String
  4.   Dim nSplitItem As Long
  5.    
  6.   Application.ScreenUpdating = False
  7.  
  8.   ' Enter items to be replaces and new ones.
  9.  strFindText = InputBox("Enter items to be found here,seperated by | (vertical bar): ", "Items to be found")
  10.   strReplaceText = InputBox("Enter new items here, seperated by | (vertical bar): ", "New items")
  11.   nSplitItem = UBound(Split(strFindText, "|"))
  12.  
  13.   ' Find each item and replace it with new one respectively.
  14.  For nSplitItem = 0 To nSplitItem
  15.     With Selection
  16.       .HomeKey Unit:=wdStory
  17.       With Selection.Find
  18.         .ClearFormatting
  19.         .Replacement.ClearFormatting
  20.         .Text = Split(strFindText, "|")(nSplitItem)
  21.         .Replacement.Text = Split(strReplaceText, "|")(nSplitItem)
  22.         .Format = False
  23.         .MatchWholeWord = False
  24.       End With
  25.     Selection.Find.Execute Replace:=wdReplaceAll
  26.   End With
  27. Next nSplitItem
  28.  
  29.   Application.ScreenUpdating = True
  30.  
  31. End Sub

Try that, it uses | instead. (Not tested since I'm on a tablet.)

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Multiple Find and replace in a word file
« Reply #4 on: January 28, 2021, 05:45 AM »
Code: Visual Basic [Select]
  1. Sub FindAndReplaceMultiItems()
  2.   Dim strFindText As String
  3.   Dim strReplaceText As String
  4.   Dim nSplitItem As Long
  5.    
  6.   Application.ScreenUpdating = False
  7.  
  8.   ' Enter items to be replaces and new ones.
  9.  strFindText = InputBox("Enter items to be found here,seperated by | (vertical bar): ", "Items to be found")
  10.   strReplaceText = InputBox("Enter new items here, seperated by | (vertical bar): ", "New items")
  11.   nSplitItem = UBound(Split(strFindText, "|"))
  12.  
  13.   ' Find each item and replace it with new one respectively.
  14.  For nSplitItem = 0 To nSplitItem
  15.     With Selection
  16.       .HomeKey Unit:=wdStory
  17.       With Selection.Find
  18.         .ClearFormatting
  19.         .Replacement.ClearFormatting
  20.         .Text = Split(strFindText, "|")(nSplitItem)
  21.         .Replacement.Text = Split(strReplaceText, "|")(nSplitItem)
  22.         .Format = False
  23.         .MatchWholeWord = False
  24.       End With
  25.     Selection.Find.Execute Replace:=wdReplaceAll
  26.   End With
  27. Next nSplitItem
  28.  
  29.   Application.ScreenUpdating = True
  30.  
  31. 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,940
    • View Profile
    • Donate to Member
Re: Multiple Find and replace in a word file
« Reply #5 on: January 29, 2021, 11:23 AM »
Works fine !!!!!!