MS Word Find-Replace with options

Hi, all,

I’m working with a script that executes several dozen search and replace operations. I’m having to add “match case” and other limiters to a few of the terms to avoid some bad matches.

After I use “with match case” or other optional limiters, it seems to stick for the searches that follow. Here’s a sample:

tell application "Microsoft Word"
	activate
	set myFind to find object of text object of active document
	execute find myFind find text "Part A" replace with "Part^sA" replace replace all with match case
	execute find myFind find text "paragraph 1" replace with "paragraph^s1" replace replace all
end tell

The above executes the change on “paragraph 1” only when the case is matched. Is there a simple way to clear the find object of any optional settings without having to explicitly declare “without match case” on the “paragraph” line?

Thanks,

Mary

Using ‘match case’ is the equivalent to checking the check box in the dialogue, which means that it remains in that state until unchecked.

What happens in a third search line if you check it in the first search, and then uncheck it in the second search?

Also, per the word-2004-applescript-reference, you can set this separately from individual search commands. So…

Yes, that’s what I was finding, that “match case” remains in effect until I specifically set it to “without”.

I was hoping there was some kind of “clear all” that would return all of these options (whole word, match case, wildcards, etc.) to their unchecked state without having to explicitly reset each one, but maybe that’s just what I have to do.

Thanks much for your help.

Well, putting aside that in the example, the ‘match case’ setting is set on a separate line… While I don’t think there is a ‘clear all’ per se, you can set multiple properties with a single command.

set myfind to find object of text object of active document
set properties of myfind to {match case:false, match whole word:false, match wildcards:false}
execute find myfind find text "he" replace with "THE" replace replace all

Additionally, you can assign the properties to a variable and then use that to set the properties for the find.

set myfind to find object of text object of active document
-- create banks of properties, e.g. all false, all true, or a mix
set pr_allFalse to {match case:false, match whole word:false, match wildcards:false}
set pr_allTrue to {match case:true, match whole word:true, match wildcards:true}
set pr_caseF_wholeF_wildcardsT to {match case:false, match whole word:false, match wildcards:true}

-- use bank 1 (all false)
set properties of myfind to pr_allFalse
execute find myfind find text "he" replace with "THE" replace replace all

-- use bank 2 (all true)
set properties of myfind to pr_allTrue
execute find myfind find text "he" replace with "THE" replace replace all

-- use bank 3 (mix)
set properties of myfind to pr_caseF_wholeF_wildcardsT
execute find myfind find text "he" replace with "THE" replace replace all

And of course, you can use this to review what properties you’d like to set. Here is a subset of the properties:

properties of myfind

[format]{class:find, forward:true, font object:font object of find id «class ÅÌíÿ» of text object of active document of
application “Microsoft Word”, found:false, match all word forms:false, match case:true, match wildcards:false,
match sounds like:false, match whole word:false, match fuzzy:false, match byte:false,
paragraph format:paragraph format of find id «class ÅÌíÿ» of text object of active document of application “Microsoft Word”,
style:missing value, content:“he”, wrap:find stop, format:false}[/format]

A belated thanks for the above suggestions. Setting up banks of options sounds like a good plan. Thanks again for the detailed reply!