PDA

View Full Version : Solved: simplification of a multiple find replace



alexander110
10-30-2007, 05:33 PM
Hi again guys, I have recorded a macro which highlights certain words within a document. The code is below - as you can see it's very repetative, is there a way to simplify this operation? For instance, do i need to specify ".MatchSoundsLike = False" for each find replace? Or could i use a foreach statement to do all the replacements together?

thanks and sorry for such a mundane question.

Alex

[word2003, windows xp]


Options.DefaultHighlightColorIndex = wdYellow

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True

With Selection.Find
.Text = "herald" 'newspaper - needs italics
.Replacement.Text = "Herald"
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

With Selection.Find
.Text = "observer" ' looking for newspaper title - needs italics
.Replacement.Text = "observer"
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

With Selection.Find
.Text = "(<[tT]he [aA]ge>)" 'the age newspaper - needs italics
.Replacement.Text = "\1"
.Wrap = wdFindContinue
.Format = True
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

With Selection.Find
.Text = "([!.][!0-9]). ([! ^13])" 'makes double space at end of sentences but not end of paragraphs
.Replacement.Text = "\1. \2"
.Wrap = wdFindContinue
.Format = True
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

etc..

TonyJollans
10-31-2007, 06:06 AM
I'll try and make this as simple as I can :)

Firstly, even when it works properly, the macro recorder generates code that can be improved upon, if for no other reason than that it has to be simplistic to be as sure as possible it will run in whatever situation it happens to be used.

Find is an object. Find objects exist and have attributes, whether or not you are using them. The Selection, and every Range, has a Find object.

The Find object of the Selection is the one used in the User Interface and exists for the life of the Word session. Changes made to it by users are remembered, so to be sure of the settings in code you must explicitly set them. Once you have set them they will stick until your code or a user changes them. So, in your code here, you only need to specify them once.

Simply making that change will shorten your code ...

Options.DefaultHighlightColorIndex = wdYellow
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Highlight = True
With Selection.Find
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
With Selection.Find
.Text = "herald" 'newspaper - needs italics
.Replacement.Text = "Herald"
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "observer" ' looking for newspaper title - needs italics
.Replacement.Text = "observer"
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "(<[tT]he [aA]ge>)" 'the age newspaper - needs italics
.Replacement.Text = "\1"
End With
Selection.Find.Execute Replace:=wdReplaceAll
With Selection.Find
.Text = "([!.][!0-9]). ([! ^13])" 'makes double space at end of sentences but not end of paragraphs
.Replacement.Text = "\1. \2"
End With
Selection.Find.Execute Replace:=wdReplaceAll



A quick eyeballing of this code shows that few statements up front, and the Executes can be incorporated into the With blocks and that the repeated With blocks can then be combined:

Options.DefaultHighlightColorIndex = wdYellow
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Highlight = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Text = "herald" 'newspaper - needs italics
.Replacement.Text = "Herald"
.Execute Replace:=wdReplaceAll
.Text = "observer" ' looking for newspaper title - needs italics
.Replacement.Text = "observer"
.Execute Replace:=wdReplaceAll
.Text = "(<[tT]he [aA]ge>)" 'the age newspaper - needs italics
.Replacement.Text = "\1"
.Execute Replace:=wdReplaceAll
.Text = "([!.][!0-9]). ([! ^13])" 'makes double space at end of sentences but not end of paragraphs
.Replacement.Text = "\1. \2"
.Execute Replace:=wdReplaceAll
End With

That's probably enough for the moment, but a final word about using Ranges other than the Selection. Ranges are generally preferable because, with the Selection, you are operating on the document through the user's window, whereas with other Ranges you are operating on the document more directly.

Ranges are transient; they do not exist all the time in the way the Selection does. When you use one, it is created, and when created, all the options (such as those of the Find object) are initialised. So, when you use the Find object of a Range you normally only need to specify those few options you want non-default values for.

fumei
10-31-2007, 11:29 AM
Is there a reason for setting the highlight color index, but never using it?

Also, I am not understanding the use of: .Text = "observer" ' looking for newspaper title - needs italics
.Replacement.Text = "observer" The Text and replacement text are identical. There is no check for italics.

TonyJollans
10-31-2007, 12:30 PM
There you go, reading the code again :)

Funny, isn't it, as you said in another thread. I looked at the mechanics of the code; you looked at the substance of it. Many eyes make light work - or something of the sort :)

fumei
10-31-2007, 12:33 PM
Darn that actually reading the code thing.

alexander110
10-31-2007, 08:56 PM
Thanks for your help with this - isn't simplified code a beautiful thing? I got a tingly feeling all over just reading your response (that's just me). It runs well - only i found i needed to add a Selection.HomeKey Unit:=wdStory to the start of this otherwise for some reason it only made changes from the cursor onwards (that is despite the .Wrap = wdFindContinue statement which I thought would have achieved the same thing.

i set DefaultHighlightColorIndex = wdYellow incase user had set her highlight pen to "no highlight", though had not checked whether this was really neccessary. As for not replacing with itallics, that was a misleading comment I had left in there - the find replace is only highlighting certain keywods and the itallicising of text is left up the user - in this case "observer" might find "medical observer" "sunday observer" or numerous other journals or words all of which would be treated differently and not neccessarily highlighted. It's a blunt tool i guess.

alex :friends: