Word for Mac

I have a filemaker database from which I merge data into word templates.
I am trying to add an additional variable to the applescript - the variable is the last variable set in the below script and is the last find and replace command.
All the other variables work fine and populate the template.

set thePath to cell "CDBloc" of current record as string
set Startdoc to thePath&"Templates:"
set Disc to cell "Template" of current record
set thetemplate to Startdoc & Disc
set theDate to cell "Date" of current record
set thefirm to cell "Firm" of current record
set thematter to cell "Matter" of current record
set theRef to cell "Ref" of current record
set docc to cell "Save_as" of current record
set act1 to repetition 1 of cell "Disc_Activity"  of current record
set act2 to repetition 2 of cell "Disc_Activity"  of current record
set act3 to repetition 3 of cell "Disc_Activity"  of current record
set act4 to repetition 4 of cell "Disc_Activity"  of current record
set act5 to repetition 5 of cell "Disc_Activity"  of current record
set act6 to repetition 6 of cell "Disc_Activity"  of current record
set act7 to repetition 7 of cell "Disc_Activity"  of current record
set act8 to repetition 8 of cell "Disc_Activity"  of current record
set act9 to repetition 9 of cell "Disc_Activity"  of current record
set no1 to repetition 1 of cell "Disc_No_units" of current record
set no2 to repetition 2 of cell "Disc_No_units" of current record
set no3 to repetition 3 of cell "Disc_No_units" of current record
set no4 to repetition 4 of cell "Disc_No_units" of current record
set no5 to repetition 5 of cell "Disc_No_units" of current record
set no6 to repetition 6 of cell "Disc_No_units" of current record
set no7 to repetition 7 of cell "Disc_No_units" of current record
set no8 to repetition 8 of cell "Disc_No_units" of current record
set no9 to repetition 9 of cell "Disc_No_units" of current record
set un1 to repetition 1 of cell "Disc_Unit_Display" of current record
set un2 to repetition 2 of cell "Disc_Unit_Display" of current record
set un3 to repetition 3 of cell "Disc_Unit_Display" of current record
set un4 to repetition 4 of cell "Disc_Unit_Display" of current record
set un5 to repetition 5 of cell "Disc_Unit_Display" of current record
set un6 to repetition 6 of cell "Disc_Unit_Display" of current record
set un7 to repetition 7 of cell "Disc_Unit_Display" of current record
set un8 to repetition 8 of cell "Disc_Unit_Display" of current record
set un9 to repetition 9 of cell "Disc_Unit_Display" of current record
set ch1 to repetition 1 of cell "Disc_Charge" of current record
set ch2 to repetition 2 of cell "Disc_Charge" of current record
set ch3 to repetition 3 of cell "Disc_Charge" of current record
set ch4 to repetition 4 of cell "Disc_Charge" of current record
set ch5 to repetition 5 of cell "Disc_Charge" of current record
set ch6 to repetition 6 of cell "Disc_Charge" of current record
set ch7 to repetition 7 of cell "Disc_Charge" of current record
set ch8 to repetition 8 of cell "Disc_Charge" of current record
set ch9 to repetition 9 of cell "Disc_Charge" of current record
set tot to cell "Total estimate" of current record
set gst to cell "Estimate GST" of current record
set GT to cell "Grand Total Est" of current record
set Dly to cell "Daily_Rate" of current record
set Hly to cell "Hourly_Rate" of current record
set typ to cell "PIterm" of current record



tell application "Microsoft Word"
	activate
	open thetemplate
save as active document file name docc

	end tell

tell application "Microsoft Word"
   set findRange to find object of selection
   tell findRange
execute find find text "<Date>" replace with thedate replace replace all
execute find find text "<Firm>" replace with thefirm replace replace all
execute find find text "<zf>" replace with theRef replace replace all
execute find find text "<Matter>" replace with thematter replace replace all
execute find find text "<A1" replace with act1 replace replace all
execute find find text "<A2" replace with act2 replace replace all
execute find find text "<A3" replace with act3 replace replace all
execute find find text "<A4" replace with act4 replace replace all
execute find find text "<A5" replace with act5 replace replace all
execute find find text "<A6" replace with act6 replace replace all
execute find find text "<A7" replace with act7 replace replace all
execute find find text "<A8" replace with act8 replace replace all
execute find find text "<A9" replace with act9 replace replace all
execute find find text "<U1" replace with no1 replace replace all
execute find find text "<U2" replace with no2 replace replace all
execute find find text "<U3" replace with no3 replace replace all
execute find find text "<U4" replace with no4 replace replace all
execute find find text "<U5" replace with no5 replace replace all
execute find find text "<U6" replace with no6 replace replace all
execute find find text "<U7" replace with no7 replace replace all
execute find find text "<U8" replace with no8 replace replace all
execute find find text "<U9" replace with no9 replace replace all
execute find find text "<N1" replace with un1 replace replace all
execute find find text "<N2" replace with un2 replace replace all
execute find find text "<N3" replace with un3 replace replace all
execute find find text "<N4" replace with un4 replace replace all
execute find find text "<N5" replace with un5 replace replace all
execute find find text "<N6" replace with un6 replace replace all
execute find find text "<N7" replace with un7 replace replace all
execute find find text "<N8" replace with un8 replace replace all
execute find find text "<N9" replace with un9 replace replace all
execute find find text "<C1" replace with ch1 replace replace all
execute find find text "<C2" replace with ch2 replace replace all
execute find find text "<C3" replace with ch3 replace replace all
execute find find text "<C4" replace with ch4 replace replace all
execute find find text "<C5" replace with ch5 replace replace all
execute find find text "<C6" replace with ch6 replace replace all
execute find find text "<C7" replace with ch7 replace replace all
execute find find text "<C8" replace with ch8 replace replace all
execute find find text "<C9" replace with ch9 replace replace all
execute find find text "<tot" replace with tot replace replace all
execute find find text "<gst" replace with gst replace replace all
execute find find text "<GT" replace with GT replace replace all
execute find find text "<d>" replace with Dly replace replace all
execute find find text "<h>" replace with Hly replace replace all
execute find find text "pers" replace with typ replace replace all

end tell
end tell

The error I get in filemaker is “Microsoft Word got an error: find id «data iWioB0620D81» of selection doesn’t understand the “execute find” message.” followed by a new dialog “Unknown error: -1708”
I cannot see what the issue is - can anyone else?
All and any input gratefully received as I am an amateur at this.

After a little further investigation the data iWioB0620D81 seems to vary each time i execute the script.

My understanding is that it would be useful to add a couple of instructions

tell application "Microsoft Word"
tell active document # ADDED
set findRange to find object of selection
.
end tell # findRange
end tell # current document # ADDED
end tell # Word

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) lundi 28 mars 2016 12:03:26

Can you tell me the reason for adding these?
After more trial and error I think I have identified the real issue: there must a restriction on the number of characters that can be inserted as the replacement.
I am trying to insert several paragraphs.
I have found that one line of text works perfectly but several lines triggers the same error message.
I have not been able to find this limitation documented anywhere.

It’s your sentence : The error I get in filemaker is “Microsoft Word got an error: find id «data iWioB0620D81» of selection doesn’t understand the “execute find” message.” followed by a new dialog “Unknown error: -1708” which let me think that maybe “speaking” to the selection without explicitly “speak” to the document may be the cause of the problem.

I was also puzzled by the fact that you wrote that you were “in filemaker” when the problem stroke.

As I don’t know the entire script I can’t say if there is a way to stop to “speak” to Filemaker just after extracting the useful values from the database.

Maybe stopping to “speak” to Filemaker may be done here:


set typ to cell "PIterm" of current record

end tell # stop speaking to Filemaker but maybe it requires more than one instruction

tell application "Microsoft Word"

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) lundi 28 mars 2016 23:17:27

The applescript is called (?) by a filemaker script. I am sorry I did not make myself clearer - I must have overlooked the fact I was in an applescript forum rather than a filemaker forum.
The reason I queried is because everything up until the last replace works. But I have worked out why (I think): it is because is was trying to replace with more than 255 characters.
From this I deduce that there is a 255 character limit on the replace command in applescript.
Does anyone know if this is correct?
Thanks for the input so far.

As Merdosoft products aren’t allowed to enter my machines, I can’t test.
You may easily test with this short script

set theTemplate to "your template name"
set docc to "the wanted filename"

set act3 to ""
# repeating 4 times would create a 204 characters long string
# repeating 5 times creates a 255 characters one
# repeating 6 times would create a 306 characters one
repeat 5 times
	set act3 to act3 & "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNO"
end repeat

tell application "Microsoft Word"
	activate
	open theTemplate
	save as active document file name docc
	
end tell

tell application "Microsoft Word"
	set findRange to find object of selection
	tell findRange
		execute find find text "<A3>" replace with act3 replace replace all
	end tell
end tell

If it works when you repeat 4 or 5 times but fails repeating 6 times 255 is really the limit.

If it is you may edit your code this way :


tell application "Microsoft Word"
	activate
	open thetemplate
	save as active document file name docc
	
end tell

tell application "Microsoft Word"
	set findRange to find object of selection
	my replaceLong("<Date>", findRange)
	my replaceLong("<Firm>", thefirm, findRange)
	my replaceLong("<zf>", theRef, findRange)
	my replaceLong("<Matter>", thematter, findRange)
	my replaceLong("<A1>", act1, findRange)
	my replaceLong("<A2>", act2, findRange)
	my replaceLong("<A3>", act3, findRange)
	my replaceLong("<A4>", act4, findRange)
	my replaceLong("<A5>", act5, findRange)
	my replaceLong("<A6>", act6, findRange)
	my replaceLong("<A7>", act7, findRange)
	my replaceLong("<A8>", act8, findRange)
	my replaceLong("<A9>", act9, findRange)
	my replaceLong("<U1>", no1, findRange)
	my replaceLong("<U2>", no2, findRange)
	my replaceLong("<U3>", no3, findRange)
	my replaceLong("<U4>", no4, findRange)
	my replaceLong("<U5>", no5, findRange)
	my replaceLong("<U6>", no6, findRange)
	my replaceLong("<U7>", no7, findRange)
	my replaceLong("<U8>", no8, findRange)
	my replaceLong("<U9>", no9, findRange)
	my replaceLong("<N1>", un1, findRange)
	my replaceLong("<N2>", un2, findRange)
	my replaceLong("<N3>", un3, findRange)
	my replaceLong("<N4>", un4, findRange)
	my replaceLong("<N5>", un5, findRange)
	my replaceLong("<N6>", un6, findRange)
	my replaceLong("<N7>", un7, findRange)
	my replaceLong("<N8>", un8, findRange)
	my replaceLong("<N9>", un9, findRange)
	my replaceLong("<C1>", ch1, findRange)
	my replaceLong("<C2>", ch2, findRange)
	my replaceLong("<C3>", ch3, findRange)
	my replaceLong("<C4>", ch4, findRange)
	my replaceLong("<C5>", ch5, findRange)
	my replaceLong("<C6>", ch6, findRange)
	my replaceLong("<C7>", ch7, findRange)
	my replaceLong("<C8>", ch8, findRange)
	my replaceLong("<C9>", ch9, findRange)
	my replaceLong("<tot>", tot, findRange)
	my replaceLong("<gst>", gst, findRange)
	my replaceLong("<GT>", GT, findRange)
	my replaceLong("<d>>", Dly, findRange)
	my replaceLong("<h>>", Hly, findRange)
	my replaceLong("pers>", typ, findRange)
end tell

on replaceLong(aKey, aString, findRange)
	tell application "Microsoft Word"
		repeat
			set shortLength to 255 - (count aKey)
			if (count aString) > 255 then
				set shortString to (text 1 thru shortLength of aString) & aKey
				set aString to text (shortLength + 1) thru -1 of aString
				tell findRange
					execute find find text aKey replace with shortString replace replace all
				end tell
			else
				tell findRange
					execute find find text aKey replace with aString replace replace all
				end tell
			end if
		end repeat
	end tell
end replaceLong

It will do the replacements by steps taking care of the 255 characters limit.

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) mardi 29 mars 2016 11:38:10

i can’t wait to try that beautifully elegant solution.
Thanks.