mass find and replace

FrecklePaw
2StarLounger
Posts: 130
Joined: 12 Aug 2020, 08:40

mass find and replace

Post by FrecklePaw »

Is there a way to do a 'mass' find and replace in documents? i.e. to correct several 'errors' all in one go, that always occur when I receive a document back from the AI software? e.g. the AI will write comma, where the person says the word comma and actually means ,

(it also will write in column, colon, coma, etc.) So is there a way I can enter all of these corrections into find and replace or elsewhere in one go rather than having to correct each individual common correction?

User avatar
HansV
Administrator
Posts: 78238
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: mass find and replace

Post by HansV »

I assume that the software inserts a space before the word comma, colon etc.
Here is a macro you can run. You can edit/expand the two arrays to fit your needs. Make sure that FromArray and ToArray have the same number of items.
Keep in mind that the macro doesn't look at the context, so if a word such as colon appears in the text itself, it will also be replaced with :

Code: Select all

Sub MassReplace()
    Dim FromArray
    Dim ToArray
    Dim i As Long

    FromArray = Array(" period", " comma", " semicolon", " colon", " question mark", " exclamation mark")
    ToArray = Array(".", ",", ";", ":", "?", "!")

    For i = 0 To UBound(FromArray)
        ActiveDocument.Content.Find.Execute _
            FindText:=FromArray(i), MatchCase:=False, MatchWildcards:=False, _
            Format:=False, ReplaceWith:=ToArray(i), Replace:=wdReplaceAll
    Next i
End Sub[/quote]
Best wishes,
Hans

FrecklePaw
2StarLounger
Posts: 130
Joined: 12 Aug 2020, 08:40

Re: mass find and replace

Post by FrecklePaw »

This is helpful thank you :)
I have a list of some others, e.g. full stop (to replace with .) so I will have a go at adding everything in and test it out - thanks again. Huge time-saver for me!

User avatar
HansV
Administrator
Posts: 78238
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: mass find and replace

Post by HansV »

Be careful to check the results, to make sure that you don't get unexpected/undesirable replacements.
Best wishes,
Hans

FrecklePaw
2StarLounger
Posts: 130
Joined: 12 Aug 2020, 08:40

Re: mass find and replace

Post by FrecklePaw »

Thanks Hans. I'm just adding some extra bits in and it's all fine so far, except for Mr., Mrs., and Dr. - I'd like to replace these to simply Mr, Ms, Dr without the full stop after but this line of code in the macro was black (not red) but didn't stick for some reason, it still had the period after all abbreviated greetings e.g.:

FromArray = Array(" Mr.", " Mrs.", "Ms. ")
ToArray = Array(" Mr", " Mrs", " Ms")

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15499
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: mass find and replace

Post by ChrisGreaves »

FrecklePaw wrote:
14 Jan 2022, 12:40
Is there a way to do a 'mass' find and replace in documents?
Yes, there is. I wrote one back in 1996 when I was charged with converting and cleansing one thousand old WordPerfect 5.1DOS documents to Word6.0 for Windows.
I wrote a dozen macros then realized that all of conversion and cleansing was a series of Edit-Replace macros, and so only one Edit-replace macro was needed, with a table to drive it.
I have attached a zip file with one of the remaining 18 rules tables that I have used over the years.

Don't be overawed by the tables. I always start of with one rule, get it working, then clone it to handle variations. Then I add a new rule and clone it, and so on.
On a corporate basis each department (legal, human resources, research & development) can have its own set of rules tables.

If you end up doing this task repeatedly, I suggest you think along the lines of a rules-driven macro.
Cheers
Chris
You do not have the required permissions to view the files attached to this post.
An expensive day out: Wallet and Grimace

User avatar
HansV
Administrator
Posts: 78238
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: mass find and replace

Post by HansV »

If these abbreviations occur at the beginning of a paragraph, they won't be replaced because there is no space before them. Try this version:

Code: Select all

Sub MassReplace2()
    Dim FromArray
    Dim ToArray
    Dim i As Long

    FromArray = Array("Mr.", "Mrs.", "Ms.", "Dr.")
    ToArray = Array("Mr", "Mrs", "Ms", "Dr")

    For i = 0 To UBound(FromArray)
        ActiveDocument.Content.Find.Execute _
            FindText:=FromArray(i), MatchCase:=True, MatchWildcards:=False, _
            Format:=False, ReplaceWith:=ToArray(i), Replace:=wdReplaceAll
    Next i
End Sub
Best wishes,
Hans

FrecklePaw
2StarLounger
Posts: 130
Joined: 12 Aug 2020, 08:40

Re: mass find and replace

Post by FrecklePaw »

HansV wrote:
14 Jan 2022, 15:30
If these abbreviations occur at the beginning of a paragraph, they won't be replaced because there is no space before them. Try this version:

Code: Select all

Sub MassReplace2()
    Dim FromArray
    Dim ToArray
    Dim i As Long

    FromArray = Array("Mr.", "Mrs.", "Ms.", "Dr.")
    ToArray = Array("Mr", "Mrs", "Ms", "Dr")

    For i = 0 To UBound(FromArray)
        ActiveDocument.Content.Find.Execute _
            FindText:=FromArray(i), MatchCase:=True, MatchWildcards:=False, _
            Format:=False, ReplaceWith:=ToArray(i), Replace:=wdReplaceAll
    Next i
End Sub
Thanks Hans, this has worked.

However, this morning I added a few extra bits into the from and to arrays and it is now telling me "run-time error '9': subscript out of range"

This is the macro as I have edited it:

Sub MassReplace()
Dim FromArray
Dim ToArray
Dim i As Long

FromArray = Array(" full stop", " comma", " coma", " comum", " common", " commerce", " column", " come", " come up", " commerce", " brackets", " bracket", " will", " new", " Ms.", " Mr.", " Dr.", " dot dot dot", " open", " close", " opens", " closes", " quote", " quotes", " paragraph", " subheading", " ..", " ,,", "'")
ToArray = Array(".", ",", ",", ",", ",", ",", ",", ",", ",", ",", "(", ")", "", "", " Ms", " Mr", " Dr", "", "", "", "", "", "", "^p^p", " ^p^p", ".", ","’")

For i = 0 To UBound(FromArray)
ActiveDocument.Content.Find.Execute _
FindText:=FromArray(i), MatchCase:=False, MatchWildcards:=False, _
Format:=False, ReplaceWith:=ToArray(i), Replace:=wdReplaceAll
Next i
End Sub


And when I clicked debug this section was highlighted yellow:

ActiveDocument.Content.Find.Execute _
FindText:=FromArray(i), MatchCase:=False, MatchWildcards:=False, _
Format:=False, ReplaceWith:=ToArray(i), Replace:=wdReplaceAll

User avatar
StuartR
Administrator
Posts: 12577
Joined: 16 Jan 2010, 15:49
Location: London, Europe

Re: mass find and replace

Post by StuartR »

It looks like the last term in your FromArray is "'" - and that will be the cause of your problem. Try using """" instead. This uses TWO " marks instead of the one you expect, because that is how you specify a quotation mark in a VBA string.
StuartR


User avatar
Charles Kenyon
4StarLounger
Posts: 596
Joined: 10 Jan 2016, 15:56
Location: Madison, Wisconsin

Re: mass find and replace

Post by Charles Kenyon »

See also:
Document Batch Processes: http://www.gmayor.com/document_batch_processes.htm
Batch Folder Process Add-In https://gregmaxey.com/word_tip_pages/pr ... addin.html

These add-ins provide a way to do a find and replace for multiple items in a table.