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?
mass find and replace
-
- Administrator
- Posts: 78488
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: mass find and replace
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 :
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
Hans
-
- 2StarLounger
- Posts: 130
- Joined: 12 Aug 2020, 08:40
Re: mass find and replace
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!
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!
-
- Administrator
- Posts: 78488
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: mass find and replace
Be careful to check the results, to make sure that you don't get unexpected/undesirable replacements.
Best wishes,
Hans
Hans
-
- 2StarLounger
- Posts: 130
- Joined: 12 Aug 2020, 08:40
Re: mass find and replace
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")
FromArray = Array(" Mr.", " Mrs.", "Ms. ")
ToArray = Array(" Mr", " Mrs", " Ms")
-
- PlutoniumLounger
- Posts: 15621
- Joined: 24 Jan 2010, 23:23
- Location: brings.slot.perky
Re: mass find and replace
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.
There's nothing heavier than an empty water bottle
-
- Administrator
- Posts: 78488
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: mass find and replace
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
Hans
-
- 2StarLounger
- Posts: 130
- Joined: 12 Aug 2020, 08:40
Re: mass find and replace
Thanks Hans, this has worked.HansV wrote: ↑14 Jan 2022, 15:30If 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
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
-
- Administrator
- Posts: 12605
- Joined: 16 Jan 2010, 15:49
- Location: London, Europe
Re: mass find and replace
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
-
- 5StarLounger
- Posts: 617
- Joined: 10 Jan 2016, 15:56
- Location: Madison, Wisconsin
Re: mass find and replace
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.
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.
Attorney Charles Kenyon
Templates in Microsoft Word
Modifying the QAT
The Importance of Styles in Microsoft Word
Templates in Microsoft Word
Modifying the QAT
The Importance of Styles in Microsoft Word