Friday, October 22, 2021

#1 2021-10-01 09:54:09 am

MaryQue
Member
Registered: 2020-01-08
Posts: 5

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:

Applescript:

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


Filed under: find, replace, Microsoft Word

Offline

 

#2 2021-10-01 01:05:25 pm

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 134

Re: MS Word Find-Replace with options

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…

set match case of myFind to true
execute find myFind find text 1
execute find myFind find text 2

set match case of myFind to false
execute find myFind find text 3
execute find myFind find text 4


Filed under: Word

Offline

 

#3 2021-10-04 08:27:29 am

MaryQue
Member
Registered: 2020-01-08
Posts: 5

Re: MS Word Find-Replace with options

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.

Offline

 

#4 2021-10-04 12:00:45 pm

Mockman
Member
From:: Toronto
Registered: 2020-05-27
Posts: 134

Re: MS Word Find-Replace with options

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.

Applescript:

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.

Applescript:

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:

Applescript:

properties of myfind

{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}

Last edited by Mockman (2021-10-04 12:01:25 pm)

Offline

 

#5 2021-10-10 11:46:07 am

MaryQue
Member
Registered: 2020-01-08
Posts: 5

Re: MS Word Find-Replace with options

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)