PDA

View Full Version : [SOLVED:] VBA Word macro - find and replace from a list



Brighton4444
10-05-2021, 07:30 AM
Hi

I am using a very simple code to find and replace a sentence, but there is a location name in the middle, which can be different (up to 300 versions) - is there a way to access a list using one piece of code, rather than copy and paste the code loads of times and change the variant?
I have made the following simpler for ease, but normally there would be more text to find - it's the location that is the issue.

Thanks in advance for any help




Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "arriving at Paris at ([0-9])([0-9])."
.Replacement.Text = "will arrive at Paris /1/2."
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll


Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "arriving at New York at ([0-9])([0-9])."
.Replacement.Text = "will arrive at New York /1/2."
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "arriving at Paris at ([0-9])([0-9])."
.Replacement.Text = "will arrive at Paris /1/2."
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

gmayor
10-05-2021, 09:47 PM
Seehttps://www.gmayor.com/document_batch_processes.htm (http://www.gmayor.com/document_batch_processes.htm) and the replace from table list option

Brighton4444
10-06-2021, 02:27 AM
Thanks gmayor, but I'm not sure that's what i'm after.
The sentence will be the same text, which will need to be altered either side of the destination (Paris etc...). I need to search for the sentence allowing for various destinations and then replace them all with alternative text, but keeping the destination the same as it was originally.

Brighton4444
10-06-2021, 03:42 AM
gmayor - found a way around my issue at your website under using wildcards (wouldn't let me post a link!)

Many thanks for your help